Skip to content

Commit bb0514b

Browse files
committed
style: format code using prettier and black
* prettier --write "**/*.md" * black -S .
1 parent 2d716a9 commit bb0514b

File tree

41 files changed

+228
-133
lines changed

Some content is hidden

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

41 files changed

+228
-133
lines changed

lcci/02.05.Sum 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 addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
910
dummy = cur = ListNode(0)

lcci/10.10.Rank from Stream/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def query(self, x):
2121

2222

2323
class StreamRank:
24-
2524
def __init__(self):
2625
self.tree = BinaryIndexedTree(50010)
2726

lcs/LCS 03. 主题空间/Solution.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ def find(x):
1616
else:
1717
for a, b in dirs:
1818
x, y = i + a, j + b
19-
if (grid[x][y] == '0' or grid[i][j] == grid[x][y]) and find(x * n + y) != find(i * n + j):
19+
if (grid[x][y] == '0' or grid[i][j] == grid[x][y]) and find(
20+
x * n + y
21+
) != find(i * n + j):
2022
size[find(x * n + y)] += size[find(i * n + j)]
2123
p[find(i * n + j)] = find(x * n + y)
22-
return max([size[i * n + j] for i in range(m) for j in range(n) if find(i * n + j) != find(m * n)], default=0)
24+
return max(
25+
[
26+
size[i * n + j]
27+
for i in range(m)
28+
for j in range(n)
29+
if find(i * n + j) != find(m * n)
30+
],
31+
default=0,
32+
)

solution/0000-0099/0079.Word Search/Solution.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ def exist(self, board: List[List[str]], word: str) -> bool:
33
def dfs(i, j, cur):
44
if cur == len(word):
55
return True
6-
if i < 0 or i >= m or j < 0 or j >= n or board[i][j] == '0' or word[cur] != board[i][j]:
6+
if (
7+
i < 0
8+
or i >= m
9+
or j < 0
10+
or j >= n
11+
or board[i][j] == '0'
12+
or word[cur] != board[i][j]
13+
):
714
return False
815
t = board[i][j]
916
board[i][j] = '0'

solution/0000-0099/0090.Subsets II/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def dfs(u, t):
88
t.append(nums[i])
99
dfs(i + 1, t)
1010
t.pop()
11-
11+
1212
ans = []
1313
nums.sort()
1414
dfs(0, [])

solution/0100-0199/0126.Word Ladder II/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
2+
def findLadders(
3+
self, beginWord: str, endWord: str, wordList: List[str]
4+
) -> List[List[str]]:
35
def dfs(path, cur):
46
if cur == beginWord:
57
ans.append(path[::-1])

solution/0100-0199/0127.Word Ladder/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def extend(m1, m2, q):
2525
q1, q2 = deque([beginWord]), deque([endWord])
2626
m1, m2 = {beginWord: 0}, {endWord: 0}
2727
while q1 and q2:
28-
t = extend(m1, m2, q1) if len(q1) <= len(
29-
q2) else extend(m2, m1, q2)
28+
t = extend(m1, m2, q1) if len(q1) <= len(q2) else extend(m2, m1, q2)
3029
if t != -1:
3130
return t + 1
3231
return 0

solution/0100-0199/0130.Surrounded Regions/Solution.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ def solve(self, board: List[List[str]]) -> None:
33
"""
44
Do not return anything, modify board in-place instead.
55
"""
6+
67
def dfs(i, j):
78
board[i][j] = '.'
89
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
@@ -13,7 +14,9 @@ def dfs(i, j):
1314
m, n = len(board), len(board[0])
1415
for i in range(m):
1516
for j in range(n):
16-
if board[i][j] == 'O' and (i == 0 or i == m - 1 or j == 0 or j == n - 1):
17+
if board[i][j] == 'O' and (
18+
i == 0 or i == m - 1 or j == 0 or j == n - 1
19+
):
1720
dfs(i, j)
1821
for i in range(m):
1922
for j in range(n):

solution/0300-0399/0307.Range Sum Query - Mutable/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def query(self, x):
2121

2222

2323
class NumArray:
24-
2524
def __init__(self, nums: List[int]):
2625
self.tree = BinaryIndexedTree(len(nums))
2726
for i, v in enumerate(nums, 1):

solution/0300-0399/0308.Range Sum Query 2D - Mutable/Solution.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def query(self, x):
2121

2222

2323
class NumMatrix:
24-
2524
def __init__(self, matrix: List[List[int]]):
2625
self.trees = []
2726
n = len(matrix[0])
@@ -37,7 +36,10 @@ def update(self, row: int, col: int, val: int) -> None:
3736
tree.update(col + 1, val - prev)
3837

3938
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
40-
return sum(tree.query(col2 + 1) - tree.query(col1) for tree in self.trees[row1: row2 + 1])
39+
return sum(
40+
tree.query(col2 + 1) - tree.query(col1)
41+
for tree in self.trees[row1 : row2 + 1]
42+
)
4143

4244

4345
# Your NumMatrix object will be instantiated and called as such:

solution/0300-0399/0399.Evaluate Division/Solution.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
2+
def calcEquation(
3+
self, equations: List[List[str]], values: List[float], queries: List[List[str]]
4+
) -> List[float]:
35
def find(x):
46
if p[x] != x:
57
origin = p[x]
@@ -18,4 +20,7 @@ def find(x):
1820
continue
1921
p[pa] = pb
2022
w[pa] = w[b] * v / w[a]
21-
return [-1 if c not in p or d not in p or find(c) != find(d) else w[c] / w[d] for c, d in queries]
23+
return [
24+
-1 if c not in p or d not in p or find(c) != find(d) else w[c] / w[d]
25+
for c, d in queries
26+
]

solution/0500-0599/0552.Student Attendance Record II/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def checkRecord(self, n: int) -> int:
1616
dp[i][1][2] = dp[i - 1][1][1]
1717
# P
1818
dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % mod
19-
dp[i][1][0] = (dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]) % mod
19+
dp[i][1][0] = (
20+
dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]
21+
) % mod
2022

2123
ans = 0
2224
for j in range(2):

solution/0500-0599/0564.Find the Closest Palindrome/Solution.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution:
22
def nearestPalindromic(self, n: str) -> str:
33
x = int(n)
44
l = len(n)
5-
res = {10 ** (l - 1) - 1, 10 ** l + 1}
6-
left = int(n[:(l + 1) >> 1])
5+
res = {10 ** (l - 1) - 1, 10**l + 1}
6+
left = int(n[: (l + 1) >> 1])
77
for i in range(left - 1, left + 2):
88
j = i if l % 2 == 0 else i // 10
99
while j:
@@ -14,6 +14,10 @@ def nearestPalindromic(self, n: str) -> str:
1414

1515
ans = -1
1616
for t in res:
17-
if ans == -1 or abs(t - x) < abs(ans - x) or (abs(t - x) == abs(ans - x) and t < ans):
17+
if (
18+
ans == -1
19+
or abs(t - x) < abs(ans - x)
20+
or (abs(t - x) == abs(ans - x) and t < ans)
21+
):
1822
ans = t
1923
return str(ans)

solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def bfs(i, j, x, y):
1616
if 0 <= c < m and 0 <= d < n and forest[c][d] > 0:
1717
if c * n + d not in dist or dist[c * n + d] > step + 1:
1818
dist[c * n + d] = step + 1
19-
heapq.heappush(
20-
q, (dist[c * n + d] + f(c, d, x, y), c, d))
19+
heapq.heappush(q, (dist[c * n + d] + f(c, d, x, y), c, d))
2120
return -1
2221

2322
m, n = len(forest), len(forest[0])
24-
trees = [(forest[i][j], i, j) for i in range(m)
25-
for j in range(n) if forest[i][j] > 1]
23+
trees = [
24+
(forest[i][j], i, j) for i in range(m) for j in range(n) if forest[i][j] > 1
25+
]
2626
trees.sort()
2727
i = j = 0
2828
ans = 0

solution/0600-0699/0683.K Empty Slots/Solution.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,31 @@ def update(self, x, delta):
1111
while x <= self.n:
1212
self.c[x] += delta
1313
x += BinaryIndexedTree.lowbit(x)
14-
14+
1515
def query(self, x):
1616
s = 0
1717
while x > 0:
1818
s += self.c[x]
1919
x -= BinaryIndexedTree.lowbit(x)
2020
return s
2121

22+
2223
class Solution:
2324
def kEmptySlots(self, bulbs: List[int], k: int) -> int:
2425
n = len(bulbs)
2526
tree = BinaryIndexedTree(n)
2627
for i, x in enumerate(bulbs, 1):
2728
tree.update(x, 1)
28-
case1 = x - k - 1 > 0 and tree.query(x - k - 1) - tree.query(x - k - 2) == 1 and tree.query(x - 1) - tree.query(x - k - 1) == 0
29-
case2 = x + k + 1 <= n and tree.query(x + k + 1) - tree.query(x + k) == 1 and tree.query(x + k) - tree.query(x) == 0
29+
case1 = (
30+
x - k - 1 > 0
31+
and tree.query(x - k - 1) - tree.query(x - k - 2) == 1
32+
and tree.query(x - 1) - tree.query(x - k - 1) == 0
33+
)
34+
case2 = (
35+
x + k + 1 <= n
36+
and tree.query(x + k + 1) - tree.query(x + k) == 1
37+
and tree.query(x + k) - tree.query(x) == 0
38+
)
3039
if case1 or case2:
3140
return i
3241
return -1

solution/0600-0699/0688.Knight Probability in Chessboard/Solution.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ def knightProbability(self, n: int, k: int, row: int, column: int) -> float:
77
if l == 0:
88
dp[l][i][j] = 1
99
else:
10-
for a, b in ((-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2)):
10+
for a, b in (
11+
(-2, -1),
12+
(-2, 1),
13+
(2, -1),
14+
(2, 1),
15+
(-1, -2),
16+
(-1, 2),
17+
(1, -2),
18+
(1, 2),
19+
):
1120
x, y = i + a, j + b
1221
if 0 <= x < n and 0 <= y < n:
1322
dp[l][i][j] += dp[l - 1][x][y] / 8

solution/0700-0799/0752.Open the Lock/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def next(s):
1111
res.append(''.join(s))
1212
s[i] = c
1313
return res
14-
14+
1515
def extend(m1, m2, q):
1616
for _ in range(len(q), 0, -1):
1717
p = q.popleft()
@@ -24,7 +24,7 @@ def extend(m1, m2, q):
2424
m1[t] = step + 1
2525
q.append(t)
2626
return -1
27-
27+
2828
def bfs():
2929
m1, m2 = {"0000": 0}, {target: 0}
3030
q1, q2 = deque([('0000')]), deque([(target)])
@@ -33,10 +33,10 @@ def bfs():
3333
if t != -1:
3434
return t
3535
return -1
36-
36+
3737
if target == '0000':
3838
return 0
3939
s = set(deadends)
4040
if '0000' in s:
4141
return -1
42-
return bfs()
42+
return bfs()

solution/0900-0999/0928.Minimize Malware Spread II/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ def find(x):
44
if p[x] != x:
55
p[x] = find(p[x])
66
return p[x]
7-
7+
88
def union(a, b):
99
pa, pb = find(a), find(b)
1010
if pa != pb:
1111
size[pb] += size[pa]
1212
p[pa] = pb
13-
13+
1414
n = len(graph)
1515
p = list(range(n))
1616
size = [1] * n
@@ -30,10 +30,10 @@ def union(a, b):
3030
for root in s:
3131
cnt[root] += 1
3232
mp[i] = s
33-
33+
3434
mx, ans = -1, 0
3535
for i, s in mp.items():
3636
t = sum(size[root] for root in s if cnt[root] == 1)
3737
if mx < t or mx == t and i < ans:
3838
mx, ans = t, i
39-
return ans
39+
return ans

solution/0900-0999/0930.Binary Subarrays With Sum/README_EN.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,22 @@ func numSubarraysWithSum(nums []int, goal int) int {
218218
* @return {number}
219219
*/
220220
var numSubarraysWithSum = function (nums, goal) {
221-
let i1 = 0,
222-
i2 = 0,
223-
s1 = 0,
224-
s2 = 0,
225-
j = 0,
226-
ans = 0;
227-
const n = nums.length;
228-
while (j < n) {
229-
s1 += nums[j];
230-
s2 += nums[j];
231-
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
232-
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
233-
ans += i2 - i1;
234-
++j;
235-
}
236-
return ans;
221+
let i1 = 0,
222+
i2 = 0,
223+
s1 = 0,
224+
s2 = 0,
225+
j = 0,
226+
ans = 0;
227+
const n = nums.length;
228+
while (j < n) {
229+
s1 += nums[j];
230+
s2 += nums[j];
231+
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
232+
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
233+
ans += i2 - i1;
234+
++j;
235+
}
236+
return ans;
237237
};
238238
```
239239

solution/1000-1099/1031.Maximum Sum of Two Non-Overlapping Subarrays/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) ->
1111
for i in range(n - firstLen - secondLen + 1):
1212
sm = max(sm, s[i + secondLen] - s[i])
1313
ans2 = max(sm + s[i + firstLen + secondLen] - s[i + secondLen], ans2)
14-
return max(ans1, ans2)
14+
return max(ans1, ans2)

solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
1717
if 0 <= x < n and 0 <= y < n and grid[x][y] == 0:
1818
q.append((x, y))
1919
grid[x][y] = 1
20-
return -1
20+
return -1

solution/1100-1199/1108.Defanging an IP Address/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Solution {
5959

6060
```ts
6161
function defangIPaddr(address: string): string {
62-
return address.split(".").join("[.]");
62+
return address.split('.').join('[.]');
6363
}
6464
```
6565

0 commit comments

Comments
 (0)