Skip to content

Commit 4675120

Browse files
Merge branch 'main' into main
2 parents fb1a152 + 8ecb50d commit 4675120

File tree

759 files changed

+21542
-1412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

759 files changed

+21542
-1412
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

125-Valid-Palindrome.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution:
22
def isPalindrome(self, s: str) -> bool:
3-
l, r = 0, len(s) - 1
4-
while l < r:
5-
while l < r and not self.alphanum(s[l]):
6-
l += 1
7-
while l < r and not self.alphanum(s[r]):
8-
r -= 1
9-
if s[l].lower() != s[r].lower():
3+
l = 0
4+
r = len(s)-1
5+
while l<r:
6+
if s[l].lower()==s[r].lower():
7+
l+=1
8+
r-=1
9+
continue
10+
11+
elif not (65<=ord(s[l])<=90 or 97<=ord(s[l])<=122 or 48<=ord(s[l])<=57):
12+
l+=1
13+
elif not (65<=ord(s[r])<=90 or 97<=ord(s[r])<=122 or 48<=ord(s[r])<=57):
14+
r-=1
15+
else:
1016
return False
11-
l += 1
12-
r -= 1
1317
return True
14-
15-
# Could write own alpha-numeric function
16-
def alphanum(self, c):
17-
return (ord('A') <= ord(c) <= ord('Z') or
18-
ord('a') <= ord(c) <= ord('z') or
19-
ord('0') <= ord(c) <= ord('9'))

200-Number-of-Islands.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ def numIslands(self, grid: List[List[str]]) -> int:
44
return 0
55

66
islands = 0
7-
q = collections.deque()
87
visit = set()
98
rows, cols = len(grid), len(grid[0])
109

212-Word-Search-II.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ class TrieNode:
22
def __init__(self):
33
self.children = {}
44
self.isWord = False
5+
self.refs = 0
56

67
def addWord(self, word):
78
cur = self
9+
cur.refs += 1
810
for c in word:
911
if c not in cur.children:
1012
cur.children[c] = TrieNode()
1113
cur = cur.children[c]
14+
cur.refs += 1
1215
cur.isWord = True
16+
17+
def removeWord(self, word):
18+
cur = self
19+
cur.refs -= 1
20+
for c in word:
21+
if c in cur.children:
22+
cur = cur.children[c]
23+
cur.refs -= 1
1324

1425

1526
class Solution:
@@ -24,14 +35,18 @@ def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
2435
def dfs(r, c, node, word):
2536
if (r < 0 or c < 0 or
2637
r == ROWS or c == COLS or
27-
board[r][c] not in node.children or (r, c) in visit):
38+
board[r][c] not in node.children or
39+
node.children[board[r][c]].refs < 1 or
40+
(r, c) in visit):
2841
return
2942

3043
visit.add((r, c))
3144
node = node.children[board[r][c]]
3245
word += board[r][c]
3346
if node.isWord:
47+
node.isWord = False
3448
res.add(word)
49+
root.removeWord(word)
3550

3651
dfs(r + 1, c, node, word)
3752
dfs(r - 1, c, node, word)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def removeDuplicates(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
k = 0
8+
i = 0
9+
while i < len(nums):
10+
val = nums[i]
11+
while i + 1 < len(nums) and nums[i+1] == val:
12+
nums.remove(val)
13+
k += 1
14+
i += 1
15+
return len(nums)
16+

261-Graph-Valid-Tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Problem is free on Lintcode
1+
# Problem is free on Lintcode https://www.lintcode.com/problem/178/
22
class Solution:
33
"""
44
@param n: An integer

27-Remove-Element.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def removeElement(self, nums, val):
3+
"""
4+
:type nums: List[int]
5+
:type val: int
6+
:rtype: int
7+
"""
8+
for i in range(nums.count(val)): # loop how many val is in the list
9+
nums.remove(val) # remove each val one by one
10+
return len(nums) # return len of nums

286-Walls-and-Gates.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ def addRooms(r, c):
1414
return
1515
visit.add((r, c))
1616
q.append([r, c])
17-
17+
18+
for r in range(ROWS):
19+
for c in range(COLS):
20+
if rooms[r][c] == 0:
21+
q.append([r, c])
22+
visit.add((r, c))
23+
1824
dist = 0
1925
while q:
2026
for i in range(len(q)):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class UnionFind:
2+
def __init__(self):
3+
self.f = {}
4+
5+
def findParent(self,x):
6+
y = self.f.get(x,x)
7+
if x!=y:
8+
y = self.f[x] = self.findParent(y)
9+
return y
10+
11+
def union(self,x,y):
12+
self.f[self.findParent(x)] = self.findParent(y)
13+
14+
class Solution:
15+
def countComponents(self, n: int, edges: List[List[int]]) -> int:
16+
dsu = UnionFind()
17+
for a,b in edges:
18+
dsu.union(a,b)
19+
return len(set(dsu.findParent(x) for x in range(n)))

371-Sum-of-Two-Integers.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)