Skip to content

Commit a13f89e

Browse files
committed
style: reformat python code using black
* black -S .
1 parent 389e387 commit a13f89e

File tree

486 files changed

+1462
-883
lines changed

Some content is hidden

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

486 files changed

+1462
-883
lines changed

basic/sorting/MergeSort/Main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def merge_sort(nums, left, right):
2323
while j <= right:
2424
tmp.append(nums[j])
2525
j += 1
26-
26+
2727
j = 0
2828
for i in range(left, right + 1):
2929
nums[i] = tmp[j]

lcci/01.01.Is Unique/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ def isUnique(self, astr: str) -> bool:
55
pos = ord(c) - ord('a')
66
if (bitmap & (1 << pos)) != 0:
77
return False
8-
bitmap |= (1 << pos)
8+
bitmap |= 1 << pos
99
return True

lcci/01.06.Compress String/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def compressString(self, S: str) -> str:
66
res = ''
77
while q < len(S):
88
if S[p] != S[q]:
9-
res += (S[p] + str(q - p))
9+
res += S[p] + str(q - p)
1010
p = q
1111
q += 1
12-
res += (S[p] + str(q - p))
13-
return res if len(res) < len(S) else S
12+
res += S[p] + str(q - p)
13+
return res if len(res) < len(S) else S

lcci/01.08.Zero Matrix/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def setZeroes(self, matrix: List[List[int]]) -> None:
1010
if matrix[i][j] == 0:
1111
zero_rows.add(i)
1212
zero_cols.add(j)
13-
13+
1414
for i in zero_rows:
1515
for j in range(cols):
1616
matrix[i][j] = 0
17-
17+
1818
for j in zero_cols:
1919
for i in range(rows):
2020
matrix[i][j] = 0
21-
21+
2222
return matrix

lcci/02.01.Remove Duplicate Node/Solution.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def removeDuplicateNodes(self, head: ListNode) -> ListNode:
910
if head is None or head.next is None:
@@ -18,4 +19,4 @@ def removeDuplicateNodes(self, head: ListNode) -> ListNode:
1819
cache.add(p.val)
1920
p = p.next
2021
cur.next = None
21-
return head
22+
return head

lcci/02.02.Kth Node From End of List/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def kthToLast(self, head: ListNode, k: int) -> int:
910
slow = fast = head

lcci/02.03.Delete Middle Node/Solution.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def deleteNode(self, node):
910
"""
1011
:type node: ListNode
1112
:rtype: void Do not return anything, modify node in-place instead.
1213
"""
1314
node.val = node.next.val
14-
node.next = node.next.next
15+
node.next = node.next.next

lcci/02.04.Partition List/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def partition(self, head: ListNode, x: int) -> ListNode:
910
l1, l2 = ListNode(0), ListNode(0)

lcci/02.05.Sum Lists/Solution.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
910
carry = 0
@@ -16,4 +17,4 @@ def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
1617
cur = cur.next
1718
l1 = None if not l1 else l1.next
1819
l2 = None if not l2 else l2.next
19-
return dummy.next
20+
return dummy.next

lcci/02.06.Palindrome Linked List/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def isPalindrome(self, head: ListNode) -> bool:
1919
if pre.val != head.val:
2020
return False
2121
pre, head = pre.next, head.next
22-
return True
22+
return True

lcci/02.07.Intersection of Two Linked Lists/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
910
cur1, cur2 = headA, headB

lcci/02.08.Linked List Cycle/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def detectCycle(self, head: ListNode) -> ListNode:
910
slow = fast = head

lcci/03.01.Three in One/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class TripleInOne:
2-
32
def __init__(self, stackSize: int):
43
self._capacity = stackSize
54
self._s = [[], [], []]

lcci/03.02.Min Stack/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class MinStack:
2-
32
def __init__(self):
43
"""
54
initialize your data structure here.
@@ -27,4 +26,4 @@ def getMin(self) -> int:
2726
# obj.push(val)
2827
# obj.pop()
2928
# param_3 = obj.top()
30-
# param_4 = obj.getMin()
29+
# param_4 = obj.getMin()

lcci/03.04.Implement Queue using Stacks/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class MyQueue:
2-
32
def __init__(self):
43
"""
54
Initialize your data structure here.

lcci/03.05.Sort of Stacks/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class SortedStack:
2-
32
def __init__(self):
43
self.s = []
54

lcci/03.06.Animal Shelter/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class AnimalShelf:
2-
32
def __init__(self):
43
self.cats = []
54
self.dogs = []

lcci/04.02.Minimum Height Tree/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
1011
def dfs(i, j):

lcci/04.04.Check Balance/Solution.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ def isBalanced(self, root: TreeNode) -> bool:
1111
if not root:
1212
return True
1313
l, r = self._height(root.left), self._height(root.right)
14-
return abs(l - r) < 2 and self.isBalanced(root.left) and self.isBalanced(root.right)
14+
return (
15+
abs(l - r) < 2
16+
and self.isBalanced(root.left)
17+
and self.isBalanced(root.right)
18+
)
1519

1620
def _height(self, node):
1721
if not node:

lcci/04.05.Legal Binary Search Tree/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
res, t = True, None
11+
1012
def isValidBST(self, root: TreeNode) -> bool:
1113
self.isValid(root)
1214
return self.res
@@ -20,4 +22,4 @@ def isValid(self, root):
2022
else:
2123
self.res = False
2224
return
23-
self.isValid(root.right)
25+
self.isValid(root.right)

lcci/04.06.Successor/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode:
1011
def dfs(root):

lcci/04.08.First Common Ancestor/Solution.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
9-
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
10+
def lowestCommonAncestor(
11+
self, root: TreeNode, p: TreeNode, q: TreeNode
12+
) -> TreeNode:
1013
if root is None or root == p or root == q:
1114
return root
1215
left = self.lowestCommonAncestor(root.left, p, q)
1316
right = self.lowestCommonAncestor(root.right, p, q)
14-
return right if left is None else (left if right is None else root)
17+
return right if left is None else (left if right is None else root)

lcci/04.10.Check SubTree/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
1011
def dfs(t1, t2):

lcci/04.12.Paths with Sum/Solution.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ def dfs(root, sum, flag):
44
nonlocal ans
55
if not root:
66
return 0
7-
if sum-root.val == 0:
7+
if sum - root.val == 0:
88
ans += 1
99
if flag == 0:
1010
dfs(root.left, sum, 0)
1111
dfs(root.right, sum, 0)
12-
dfs(root.left, sum-root.val, 1)
13-
dfs(root.right, sum-root.val, 1)
14-
12+
dfs(root.left, sum - root.val, 1)
13+
dfs(root.right, sum - root.val, 1)
14+
1515
if not root:
1616
return 0
1717
ans = 0
1818
dfs(root, sum, 0)
19-
return ans
19+
return ans

lcci/08.01.Three Steps Problem/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ def waysToStep(self, n: int) -> int:
55
a, b, c = 1, 2, 4
66
for _ in range(4, n + 1):
77
a, b, c = b, c, (a + b + c) % 1000000007
8-
return c
8+
return c

lcci/08.10.Color Fill/Solution.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
class Solution:
2-
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
2+
def floodFill(
3+
self, image: List[List[int]], sr: int, sc: int, newColor: int
4+
) -> List[List[int]]:
35
def dfs(i, j, oc, nc):
4-
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
6+
if (
7+
i < 0
8+
or i >= len(image)
9+
or j < 0
10+
or j >= len(image[0])
11+
or image[i][j] != oc
12+
or image[i][j] == nc
13+
):
514
return
615
image[i][j] = nc
716
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:

lcci/08.12.Eight Queens/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def dfs(u):
1717
dfs(u + 1)
1818
g[u][i] = '.'
1919
col[i] = dg[u + i] = udg[n - u + i] = False
20-
20+
2121
dfs(0)
2222
return res
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class WordsFrequency:
2-
32
def __init__(self, book: List[str]):
43
self.counter = Counter(book)
54

65
def get(self, word: str) -> int:
76
return self.counter[word]
87

8+
99
# Your WordsFrequency object will be instantiated and called as such:
1010
# obj = WordsFrequency(book)
1111
# param_1 = obj.get(word)

lcci/16.10.Living People/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ def maxAliveYear(self, birth: List[int], death: List[int]) -> int:
1212
if years[i] > max_v:
1313
max_v = years[i]
1414
res = i
15-
return 1900 + res
15+
return 1900 + res

lcci/16.25.LRU Cache/Solution.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ def __init__(self, key=0, value=0):
55
self.prev = None
66
self.next = None
77

8-
class LRUCache:
98

9+
class LRUCache:
1010
def __init__(self, capacity: int):
1111
self.cache = {}
1212
self.head = Node()
@@ -37,15 +37,15 @@ def put(self, key: int, value: int) -> None:
3737
node = self.remove_tail()
3838
self.cache.pop(node.key)
3939
self.size -= 1
40-
40+
4141
def move_to_head(self, node):
4242
self.remove_node(node)
4343
self.add_to_head(node)
44-
44+
4545
def remove_node(self, node):
4646
node.prev.next = node.next
4747
node.next.prev = node.prev
48-
48+
4949
def add_to_head(self, node):
5050
node.next = self.head.next
5151
self.head.next.prev = node
@@ -61,4 +61,4 @@ def remove_tail(self):
6161
# Your LRUCache object will be instantiated and called as such:
6262
# obj = LRUCache(capacity)
6363
# param_1 = obj.get(key)
64-
# obj.put(key,value)
64+
# obj.put(key,value)

lcci/17.05.Find Longest Subarray/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def findLongestSubarray(self, array: List[str]) -> List[str]:
88
if t in seen:
99
if mx < i - seen[t]:
1010
mx = i - seen[t]
11-
ans = array[seen[t] + 1: i + 1]
11+
ans = array[seen[t] + 1 : i + 1]
1212
else:
1313
seen[t] = i
1414
return ans

lcci/17.07.Baby Names/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ def union(a, b):
2121

2222
for e in names:
2323
idx = e.find("(")
24-
name, w = e[: idx], int(e[idx + 1: -1])
24+
name, w = e[:idx], int(e[idx + 1 : -1])
2525
mp[name] = w
2626
p[name] = name
2727
for e in synonyms:
2828
idx = e.find(",")
29-
name1, name2 = e[1: idx], e[idx + 1: -1]
29+
name1, name2 = e[1:idx], e[idx + 1 : -1]
3030
mp[name1] += 0
3131
mp[name2] += 0
3232
p[name1] = name1
3333
p[name2] = name2
3434

3535
for e in synonyms:
3636
idx = e.find(",")
37-
name1, name2 = e[1: idx], e[idx + 1: -1]
37+
name1, name2 = e[1:idx], e[idx + 1 : -1]
3838
union(name1, name2)
3939
return [f'{name}({mp[name]})' for name, w in mp.items() if name == find(name)]

lcci/17.10.Find Majority Element/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ def majorityElement(self, nums: List[int]) -> int:
44
for num in nums:
55
if cnt == 0:
66
candidate = num
7-
cnt += (1 if candidate == num else -1)
7+
cnt += 1 if candidate == num else -1
88
return candidate if nums.count(candidate) > len(nums) / 2 else -1

lcci/17.12.BiNode/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution:
910
def convertBiNode(self, root: TreeNode) -> TreeNode:
1011
if root is None:

0 commit comments

Comments
 (0)