Skip to content

Commit a6650f1

Browse files
committed
🎨 Format files (🛠️ from Github Actions)
1 parent 40f6c6b commit a6650f1

File tree

177 files changed

+876
-750
lines changed

Some content is hidden

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

177 files changed

+876
-750
lines changed

1-Two-Sum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def twoSum(self, nums: List[int], target: int) -> List[int]:
3-
prevMap = {} # val -> index
4-
3+
prevMap = {} # val -> index
4+
55
for i, n in enumerate(nums):
66
diff = target - n
77
if diff in prevMap:

10-Regular-Expression-Matching.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,44 @@ class Solution:
33
def isMatch(self, s: str, p: str) -> bool:
44
cache = [[False] * (len(p) + 1) for i in range(len(s) + 1)]
55
cache[len(s)][len(p)] = True
6-
6+
77
for i in range(len(s), -1, -1):
8-
for j in range(len(p) - 1, -1 ,-1):
8+
for j in range(len(p) - 1, -1, -1):
99
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
10-
10+
1111
if (j + 1) < len(p) and p[j + 1] == "*":
1212
cache[i][j] = cache[i][j + 2]
1313
if match:
1414
cache[i][j] = cache[i + 1][j] or cache[i][j]
1515
elif match:
16-
cache[i][j] = cache[i+1][j+1]
17-
16+
cache[i][j] = cache[i + 1][j + 1]
17+
1818
return cache[0][0]
19-
20-
19+
20+
2121
# TOP DOWN MEMOIZATION
2222
class Solution:
2323
def isMatch(self, s: str, p: str) -> bool:
2424
cache = {}
25-
25+
2626
def dfs(i, j):
2727
if (i, j) in cache:
2828
return cache[(i, j)]
2929
if i >= len(s) and j >= len(p):
3030
return True
3131
if j >= len(p):
3232
return False
33-
33+
3434
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
3535
if (j + 1) < len(p) and p[j + 1] == "*":
36-
cache[(i, j)] = (dfs(i, j + 2) or # dont use *
37-
(match and dfs(i + 1, j))) # use *
36+
cache[(i, j)] = dfs(i, j + 2) or ( # dont use *
37+
match and dfs(i + 1, j)
38+
) # use *
3839
return cache[(i, j)]
3940
if match:
40-
cache[(i,j)] = dfs(i + 1, j + 1)
41-
return cache[(i,j)]
42-
cache[(i,j)] = False
41+
cache[(i, j)] = dfs(i + 1, j + 1)
42+
return cache[(i, j)]
43+
cache[(i, j)] = False
4344
return False
44-
45+
4546
return dfs(0, 0)

100-Same-Tree.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
10-
if not p and not q: return True
11-
if p and q and p.val == q.val:
11+
if not p and not q:
12+
return True
13+
if p and q and p.val == q.val:
1214
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
13-
else: return False
15+
else:
16+
return False

102-Binary-Tree-Level-Order-Traversal.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
def levelOrder(self, root: TreeNode) -> List[List[int]]:
1011
res = []
1112
q = collections.deque()
12-
if root: q.append(root)
13-
13+
if root:
14+
q.append(root)
15+
1416
while q:
1517
val = []
16-
18+
1719
for i in range(len(q)):
1820
node = q.popleft()
1921
val.append(node.val)
20-
if node.left: q.append(node.left)
21-
if node.right: q.append(node.right)
22+
if node.left:
23+
q.append(node.left)
24+
if node.right:
25+
q.append(node.right)
2226
res.append(val)
2327
return res

104-Maximum-Depth-of-Binary-Tree.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@ class Solution:
33
def maxDepth(self, root: TreeNode) -> int:
44
if not root:
55
return 0
6-
6+
77
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
88

9-
# ITERATIVE DFS
9+
10+
# ITERATIVE DFS
1011
class Solution:
1112
def maxDepth(self, root: TreeNode) -> int:
1213
stack = [[root, 1]]
1314
res = 0
14-
15+
1516
while stack:
1617
node, depth = stack.pop()
17-
18+
1819
if node:
1920
res = max(res, depth)
2021
stack.append([node.left, depth + 1])
2122
stack.append([node.right, depth + 1])
2223
return res
2324

25+
2426
# BFS
2527
class Solution:
2628
def maxDepth(self, root: TreeNode) -> int:
2729
if not root:
2830
return 0
29-
31+
3032
level = 0
3133
q = deque([root])
3234
while q:
33-
35+
3436
for i in range(len(q)):
3537
node = q.popleft()
3638
if node.left:

1046-Last-Stone-Weight.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ class Solution:
22
def lastStoneWeight(self, stones: List[int]) -> int:
33
stones = [-s for s in stones]
44
heapq.heapify(stones)
5-
5+
66
while len(stones) > 1:
77
first = heapq.heappop(stones)
88
second = heapq.heappop(stones)
99
if second > first:
1010
heapq.heappush(stones, first - second)
11-
11+
1212
stones.append(0)
1313
return abs(stones[0])
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
class Solution:
22
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
33
inorder_index_mapping = {}
4-
for k,v in enumerate(inorder):
4+
for k, v in enumerate(inorder):
55
inorder_index_mapping[v] = k
66
index = 0
7-
def buildtree(left,right):
7+
8+
def buildtree(left, right):
89
nonlocal index
9-
if left>right:
10+
if left > right:
1011
return None
11-
root_val=preorder[index]
12+
root_val = preorder[index]
1213
root = TreeNode(root_val)
13-
index+=1
14-
root.left = buildtree(left, inorder_index_mapping[root_val]-1)
15-
root.right = buildtree(inorder_index_mapping[root_val]+1, right)
14+
index += 1
15+
root.left = buildtree(left, inorder_index_mapping[root_val] - 1)
16+
root.right = buildtree(inorder_index_mapping[root_val] + 1, right)
1617
return root
17-
return buildtree(0, len(preorder)-1)
18+
19+
return buildtree(0, len(preorder) - 1)

11-Container-With-Most-Water.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def maxArea(self, height: List[int]) -> int:
33
l, r = 0, len(height) - 1
44
res = 0
5-
5+
66
while l < r:
77
res = max(res, min(height[l], height[r]) * (r - l))
88
if height[l] < height[r]:

110-Balanced-Binary-Tree.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
# self.right = right
77
class Solution:
88
def isBalanced(self, root: Optional[TreeNode]) -> bool:
9-
109
def dfs(root):
11-
if not root: return [True, 0]
12-
10+
if not root:
11+
return [True, 0]
12+
1313
left, right = dfs(root.left), dfs(root.right)
14-
balanced = (left[0] and right[0] and
15-
abs(left[1] - right[1]) <= 1)
14+
balanced = left[0] and right[0] and abs(left[1] - right[1]) <= 1
1615
return [balanced, 1 + max(left[1], right[1])]
16+
1717
return dfs(root)[0]

1143-Longest-Common-Subsequence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
33
dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]
4-
4+
55
for i in range(len(text1) - 1, -1, -1):
66
for j in range(len(text2) - 1, -1, -1):
77
if text1[i] == text2[j]:
88
dp[i][j] = 1 + dp[i + 1][j + 1]
99
else:
1010
dp[i][j] = max(dp[i][j + 1], dp[i + 1][j])
11-
11+
1212
return dp[0][0]

0 commit comments

Comments
 (0)