Skip to content

Commit 04c4981

Browse files
committed
feat: update python solutions to lc problems
1 parent 13351b3 commit 04c4981

File tree

48 files changed

+442
-442
lines changed

Some content is hidden

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

48 files changed

+442
-442
lines changed

lcof2/剑指 Offer II 044. 二叉树每层的最大值/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Solution:
9797
ans = []
9898
while q:
9999
t = float('-inf')
100-
for _ in range(len(q), 0, -1):
100+
for _ in range(len(q)):
101101
node = q.popleft()
102102
t = max(t, node.val)
103103
if node.left:
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Definition for a binary tree node.
2-
# class TreeNode:
3-
# def __init__(self, val=0, left=None, right=None):
4-
# self.val = val
5-
# self.left = left
6-
# self.right = right
7-
class Solution:
8-
def largestValues(self, root: TreeNode) -> List[int]:
9-
if root is None:
10-
return []
11-
q = deque([root])
12-
ans = []
13-
while q:
14-
t = float('-inf')
15-
for _ in range(len(q), 0, -1):
16-
node = q.popleft()
17-
t = max(t, node.val)
18-
if node.left:
19-
q.append(node.left)
20-
if node.right:
21-
q.append(node.right)
22-
ans.append(t)
23-
return ans
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def largestValues(self, root: TreeNode) -> List[int]:
9+
if root is None:
10+
return []
11+
q = deque([root])
12+
ans = []
13+
while q:
14+
t = float('-inf')
15+
for _ in range(len(q)):
16+
node = q.popleft()
17+
t = max(t, node.val)
18+
if node.left:
19+
q.append(node.left)
20+
if node.right:
21+
q.append(node.right)
22+
ans.append(t)
23+
return ans

solution/0100-0199/0127.Word Ladder/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ while q1 and q2:
6969

7070
def extend(m1, m2, q):
7171
# 新一轮扩展
72-
for _ in range(len(q), 0, -1):
72+
for _ in range(len(q)):
7373
p = q.popleft()
7474
step = m1[p]
7575
for t in next(p):
@@ -99,7 +99,7 @@ class Solution:
9999
ans = 1
100100
while q:
101101
ans += 1
102-
for _ in range(len(q), 0, -1):
102+
for _ in range(len(q)):
103103
s = q.popleft()
104104
s = list(s)
105105
for i in range(len(s)):
@@ -123,7 +123,7 @@ class Solution:
123123
class Solution:
124124
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
125125
def extend(m1, m2, q):
126-
for _ in range(len(q), 0, -1):
126+
for _ in range(len(q)):
127127
s = q.popleft()
128128
step = m1[s]
129129
s = list(s)

solution/0100-0199/0127.Word Ladder/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Solution:
6060
ans = 1
6161
while q:
6262
ans += 1
63-
for _ in range(len(q), 0, -1):
63+
for _ in range(len(q)):
6464
s = q.popleft()
6565
s = list(s)
6666
for i in range(len(s)):
@@ -82,7 +82,7 @@ class Solution:
8282
class Solution:
8383
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
8484
def extend(m1, m2, q):
85-
for _ in range(len(q), 0, -1):
85+
for _ in range(len(q)):
8686
s = q.popleft()
8787
step = m1[s]
8888
s = list(s)
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
class Solution:
2-
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
3-
def extend(m1, m2, q):
4-
for _ in range(len(q), 0, -1):
5-
s = q.popleft()
6-
step = m1[s]
7-
s = list(s)
8-
for i in range(len(s)):
9-
ch = s[i]
10-
for j in range(26):
11-
s[i] = chr(ord('a') + j)
12-
t = ''.join(s)
13-
if t in m1 or t not in words:
14-
continue
15-
if t in m2:
16-
return step + 1 + m2[t]
17-
m1[t] = step + 1
18-
q.append(t)
19-
s[i] = ch
20-
return -1
21-
22-
words = set(wordList)
23-
if endWord not in words:
24-
return 0
25-
q1, q2 = deque([beginWord]), deque([endWord])
26-
m1, m2 = {beginWord: 0}, {endWord: 0}
27-
while q1 and q2:
28-
t = extend(m1, m2, q1) if len(q1) <= len(q2) else extend(m2, m1, q2)
29-
if t != -1:
30-
return t + 1
31-
return 0
1+
class Solution:
2+
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
3+
def extend(m1, m2, q):
4+
for _ in range(len(q)):
5+
s = q.popleft()
6+
step = m1[s]
7+
s = list(s)
8+
for i in range(len(s)):
9+
ch = s[i]
10+
for j in range(26):
11+
s[i] = chr(ord('a') + j)
12+
t = ''.join(s)
13+
if t in m1 or t not in words:
14+
continue
15+
if t in m2:
16+
return step + 1 + m2[t]
17+
m1[t] = step + 1
18+
q.append(t)
19+
s[i] = ch
20+
return -1
21+
22+
words = set(wordList)
23+
if endWord not in words:
24+
return 0
25+
q1, q2 = deque([beginWord]), deque([endWord])
26+
m1, m2 = {beginWord: 0}, {endWord: 0}
27+
while q1 and q2:
28+
t = extend(m1, m2, q1) if len(q1) <= len(q2) else extend(m2, m1, q2)
29+
if t != -1:
30+
return t + 1
31+
return 0

solution/0200-0299/0286.Walls and Gates/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Solution:
8484
d = 0
8585
while q:
8686
d += 1
87-
for _ in range(len(q), 0, -1):
87+
for _ in range(len(q)):
8888
i, j = q.popleft()
8989
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
9090
x, y = i + a, j + b

solution/0200-0299/0286.Walls and Gates/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Solution:
6060
d = 0
6161
while q:
6262
d += 1
63-
for _ in range(len(q), 0, -1):
63+
for _ in range(len(q)):
6464
i, j = q.popleft()
6565
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
6666
x, y = i + a, j + b
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
class Solution:
2-
def wallsAndGates(self, rooms: List[List[int]]) -> None:
3-
"""
4-
Do not return anything, modify rooms in-place instead.
5-
"""
6-
m, n = len(rooms), len(rooms[0])
7-
inf = 2**31 - 1
8-
q = deque([(i, j) for i in range(m) for j in range(n) if rooms[i][j] == 0])
9-
d = 0
10-
while q:
11-
d += 1
12-
for _ in range(len(q), 0, -1):
13-
i, j = q.popleft()
14-
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
15-
x, y = i + a, j + b
16-
if 0 <= x < m and 0 <= y < n and rooms[x][y] == inf:
17-
rooms[x][y] = d
18-
q.append((x, y))
1+
class Solution:
2+
def wallsAndGates(self, rooms: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify rooms in-place instead.
5+
"""
6+
m, n = len(rooms), len(rooms[0])
7+
inf = 2**31 - 1
8+
q = deque([(i, j) for i in range(m) for j in range(n) if rooms[i][j] == 0])
9+
d = 0
10+
while q:
11+
d += 1
12+
for _ in range(len(q)):
13+
i, j = q.popleft()
14+
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
15+
x, y = i + a, j + b
16+
if 0 <= x < m and 0 <= y < n and rooms[x][y] == inf:
17+
rooms[x][y] = d
18+
q.append((x, y))

solution/0300-0399/0317.Shortest Distance from All Buildings/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Solution:
9393
vis = set()
9494
while q:
9595
d += 1
96-
for _ in range(len(q), 0, -1):
96+
for _ in range(len(q)):
9797
r, c = q.popleft()
9898
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
9999
x, y = r + a, c + b

solution/0300-0399/0317.Shortest Distance from All Buildings/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Solution:
8181
vis = set()
8282
while q:
8383
d += 1
84-
for _ in range(len(q), 0, -1):
84+
for _ in range(len(q)):
8585
r, c = q.popleft()
8686
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
8787
x, y = r + a, c + b
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
class Solution:
2-
def shortestDistance(self, grid: List[List[int]]) -> int:
3-
m, n = len(grid), len(grid[0])
4-
q = deque()
5-
total = 0
6-
cnt = [[0] * n for _ in range(m)]
7-
dist = [[0] * n for _ in range(m)]
8-
for i in range(m):
9-
for j in range(n):
10-
if grid[i][j] == 1:
11-
total += 1
12-
q.append((i, j))
13-
d = 0
14-
vis = set()
15-
while q:
16-
d += 1
17-
for _ in range(len(q), 0, -1):
18-
r, c = q.popleft()
19-
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
20-
x, y = r + a, c + b
21-
if (
22-
0 <= x < m
23-
and 0 <= y < n
24-
and grid[x][y] == 0
25-
and (x, y) not in vis
26-
):
27-
cnt[x][y] += 1
28-
dist[x][y] += d
29-
q.append((x, y))
30-
vis.add((x, y))
31-
ans = float('inf')
32-
for i in range(m):
33-
for j in range(n):
34-
if grid[i][j] == 0 and cnt[i][j] == total:
35-
ans = min(ans, dist[i][j])
36-
return -1 if ans == float('inf') else ans
1+
class Solution:
2+
def shortestDistance(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
q = deque()
5+
total = 0
6+
cnt = [[0] * n for _ in range(m)]
7+
dist = [[0] * n for _ in range(m)]
8+
for i in range(m):
9+
for j in range(n):
10+
if grid[i][j] == 1:
11+
total += 1
12+
q.append((i, j))
13+
d = 0
14+
vis = set()
15+
while q:
16+
d += 1
17+
for _ in range(len(q)):
18+
r, c = q.popleft()
19+
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
20+
x, y = r + a, c + b
21+
if (
22+
0 <= x < m
23+
and 0 <= y < n
24+
and grid[x][y] == 0
25+
and (x, y) not in vis
26+
):
27+
cnt[x][y] += 1
28+
dist[x][y] += d
29+
q.append((x, y))
30+
vis.add((x, y))
31+
ans = float('inf')
32+
for i in range(m):
33+
for j in range(n):
34+
if grid[i][j] == 0 and cnt[i][j] == total:
35+
ans = min(ans, dist[i][j])
36+
return -1 if ans == float('inf') else ans

solution/0400-0499/0417.Pacific Atlantic Water Flow/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Solution:
6060
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
6161
def bfs(q, vis):
6262
while q:
63-
for _ in range(len(q), 0, -1):
63+
for _ in range(len(q)):
6464
i, j = q.popleft()
6565
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
6666
x, y = i + a, j + b

solution/0400-0499/0417.Pacific Atlantic Water Flow/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Solution:
4848
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
4949
def bfs(q, vis):
5050
while q:
51-
for _ in range(len(q), 0, -1):
51+
for _ in range(len(q)):
5252
i, j = q.popleft()
5353
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
5454
x, y = i + a, j + b

0 commit comments

Comments
 (0)