Skip to content

Commit 29ae471

Browse files
committed
feat: update solutions to lc problems
1 parent 1614694 commit 29ae471

File tree

16 files changed

+62
-44
lines changed

16 files changed

+62
-44
lines changed

lcci/04.01.Route Between Nodes/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def findWhetherExistsPath(self, n: int, graph: List[List[int]], start: int, target: int) -> bool:
2+
def findWhetherExistsPath(
3+
self, n: int, graph: List[List[int]], start: int, target: int
4+
) -> bool:
35
g = defaultdict(list)
46
for u, v in graph:
57
g[u].append(v)

lcof2/剑指 Offer II 107. 矩阵中的距离/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
多源 BFS
52+
**方法一:多源 BFS**
5353

5454
初始化结果矩阵 ans,所有 0 的距离为 0,所以 1 的距离为 -1。初始化队列 q 存储 BFS 需要检查的位置,并将所有 0 的位置入队。
5555

@@ -67,9 +67,9 @@ class Solution:
6767
m, n = len(mat), len(mat[0])
6868
ans = [[-1] * n for _ in range(m)]
6969
q = deque()
70-
for i in range(m):
71-
for j in range(n):
72-
if mat[i][j] == 0:
70+
for i, row in enumerate(mat):
71+
for j, v in enumerate(row):
72+
if v == 0:
7373
ans[i][j] = 0
7474
q.append((i, j))
7575
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
@@ -178,9 +178,9 @@ func updateMatrix(mat [][]int) [][]int {
178178
}
179179
type pair struct{ x, y int }
180180
var q []pair
181-
for i := 0; i < m; i++ {
182-
for j := 0; j < n; j++ {
183-
if mat[i][j] == 0 {
181+
for i, row := range mat {
182+
for j, v := range row {
183+
if v == 0 {
184184
ans[i][j] = 0
185185
q = append(q, pair{i, j})
186186
}

lcof2/剑指 Offer II 107. 矩阵中的距离/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ func updateMatrix(mat [][]int) [][]int {
99
}
1010
type pair struct{ x, y int }
1111
var q []pair
12-
for i := 0; i < m; i++ {
13-
for j := 0; j < n; j++ {
14-
if mat[i][j] == 0 {
12+
for i, row := range mat {
13+
for j, v := range row {
14+
if v == 0 {
1515
ans[i][j] = 0
1616
q = append(q, pair{i, j})
1717
}

lcof2/剑指 Offer II 107. 矩阵中的距离/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
33
m, n = len(mat), len(mat[0])
44
ans = [[-1] * n for _ in range(m)]
55
q = deque()
6-
for i in range(m):
7-
for j in range(n):
8-
if mat[i][j] == 0:
6+
for i, row in enumerate(mat):
7+
for j, v in enumerate(row):
8+
if v == 0:
99
ans[i][j] = 0
1010
q.append((i, j))
1111
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]

solution/0500-0599/0542.01 Matrix/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
多源 BFS
50+
**方法一:多源 BFS**
5151

5252
初始化结果矩阵 ans,所有 0 的距离为 0,所以 1 的距离为 -1。初始化队列 q 存储 BFS 需要检查的位置,并将所有 0 的位置入队。
5353

@@ -65,9 +65,9 @@ class Solution:
6565
m, n = len(mat), len(mat[0])
6666
ans = [[-1] * n for _ in range(m)]
6767
q = deque()
68-
for i in range(m):
69-
for j in range(n):
70-
if mat[i][j] == 0:
68+
for i, row in enumerate(mat):
69+
for j, v in enumerate(row):
70+
if v == 0:
7171
ans[i][j] = 0
7272
q.append((i, j))
7373
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
@@ -176,9 +176,9 @@ func updateMatrix(mat [][]int) [][]int {
176176
}
177177
type pair struct{ x, y int }
178178
var q []pair
179-
for i := 0; i < m; i++ {
180-
for j := 0; j < n; j++ {
181-
if mat[i][j] == 0 {
179+
for i, row := range mat {
180+
for j, v := range row {
181+
if v == 0 {
182182
ans[i][j] = 0
183183
q = append(q, pair{i, j})
184184
}

solution/0500-0599/0542.01 Matrix/README_EN.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class Solution:
4747
m, n = len(mat), len(mat[0])
4848
ans = [[-1] * n for _ in range(m)]
4949
q = deque()
50-
for i in range(m):
51-
for j in range(n):
52-
if mat[i][j] == 0:
50+
for i, row in enumerate(mat):
51+
for j, v in enumerate(row):
52+
if v == 0:
5353
ans[i][j] = 0
5454
q.append((i, j))
5555
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
@@ -156,9 +156,9 @@ func updateMatrix(mat [][]int) [][]int {
156156
}
157157
type pair struct{ x, y int }
158158
var q []pair
159-
for i := 0; i < m; i++ {
160-
for j := 0; j < n; j++ {
161-
if mat[i][j] == 0 {
159+
for i, row := range mat {
160+
for j, v := range row {
161+
if v == 0 {
162162
ans[i][j] = 0
163163
q = append(q, pair{i, j})
164164
}

solution/0500-0599/0542.01 Matrix/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ func updateMatrix(mat [][]int) [][]int {
99
}
1010
type pair struct{ x, y int }
1111
var q []pair
12-
for i := 0; i < m; i++ {
13-
for j := 0; j < n; j++ {
14-
if mat[i][j] == 0 {
12+
for i, row := range mat {
13+
for j, v := range row {
14+
if v == 0 {
1515
ans[i][j] = 0
1616
q = append(q, pair{i, j})
1717
}

solution/0800-0899/0854.K-Similar Strings/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def next(s):
77
i += 1
88
for j in range(i + 1, n):
99
if s[j] == s2[i] and s[j] != s2[j]:
10-
res.append(s[:i] + s[j] + s[i + 1: j] + s[i] + s[j + 1:])
10+
res.append(s[:i] + s[j] + s[i + 1 : j] + s[i] + s[j + 1 :])
1111
return res
1212

1313
q = deque([s1])

solution/0800-0899/0864.Shortest Path to Get All Keys/Solution.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def shortestPathAllKeys(self, grid: List[str]) -> int:
2121
nxt = state
2222
x, y = i + dirs[k], j + dirs[k + 1]
2323
if 0 <= x < m and 0 <= y < n and grid[x][y] != '#':
24-
if grid[x][y].isupper() and (nxt & (1 << (ord(grid[x][y]) - ord('A')))) == 0:
24+
if (
25+
grid[x][y].isupper()
26+
and (nxt & (1 << (ord(grid[x][y]) - ord('A')))) == 0
27+
):
2528
continue
2629
if grid[x][y].islower():
2730
nxt |= 1 << (ord(grid[x][y]) - ord('a'))

solution/1200-1299/1210.Minimum Moves to Reach Target with Rotations/Solution.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ def check(a, b):
1717
return ans
1818
i1, j1 = a // n, a % n
1919
i2, j2 = b // n, b % n
20-
if j1 + 1 < n and j2 + 1 < n and grid[i1][j1 + 1] == 0 and grid[i2][j2 + 1] == 0:
20+
if (
21+
j1 + 1 < n
22+
and j2 + 1 < n
23+
and grid[i1][j1 + 1] == 0
24+
and grid[i2][j2 + 1] == 0
25+
):
2126
check(i1 * n + j1 + 1, i2 * n + j2 + 1)
2227
if j1 == j2:
2328
check(a, i1 * n + j2 + 1)
24-
if i1 + 1 < n and i2 + 1 < n and grid[i1 + 1][j1] == 0 and grid[i2 + 1][j2] == 0:
29+
if (
30+
i1 + 1 < n
31+
and i2 + 1 < n
32+
and grid[i1 + 1][j1] == 0
33+
and grid[i2 + 1][j2] == 0
34+
):
2535
check((i1 + 1) * n + j1, (i2 + 1) * n + j2)
2636
if i1 == i2:
2737
check(a, (i2 + 1) * n + j1)

solution/1200-1299/1284.Minimum Number of Flips to Convert Binary Matrix to Zero Matrix/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def minFlips(self, mat: List[List[int]]) -> int:
33
m, n = len(mat), len(mat[0])
4-
state = sum(1 << (i * n + j) for i in range(m)
5-
for j in range(n) if mat[i][j])
4+
state = sum(1 << (i * n + j) for i in range(m) for j in range(n) if mat[i][j])
65
q = deque([state])
76
vis = set([state])
87
ans = 0

solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.py

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

solution/1700-1799/1778.Shortest Path in a Hidden Grid/Solution.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#
1414
#
1515

16+
1617
class Solution(object):
1718
def findShortestPath(self, master: 'GridMaster') -> int:
1819
def dfs(i, j):
@@ -29,8 +30,12 @@ def dfs(i, j):
2930

3031
target = None
3132
s = set()
32-
dirs = [['U', 'D', -1, 0], ['D', 'U', 1, 0],
33-
['L', 'R', 0, -1], ['R', 'L', 0, 1]]
33+
dirs = [
34+
['U', 'D', -1, 0],
35+
['D', 'U', 1, 0],
36+
['L', 'R', 0, -1],
37+
['R', 'L', 0, 1],
38+
]
3439
dfs(0, 0)
3540
if target is None:
3641
return -1

solution/1900-1999/1993.Operations on Tree/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class LockingTree:
2-
32
def __init__(self, parent: List[int]):
43
self.nums = {}
54
self.parent = parent

solution/2000-2099/2092.Find All People With Secret/Solution.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
2+
def findAllPeople(
3+
self, n: int, meetings: List[List[int]], firstPerson: int
4+
) -> List[int]:
35
vis = [False] * n
46
vis[0] = vis[firstPerson] = True
57
meetings.sort(key=lambda x: x[2])
@@ -10,7 +12,7 @@ def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) ->
1012
j += 1
1113
s = set()
1214
g = defaultdict(list)
13-
for x, y, _ in meetings[i: j + 1]:
15+
for x, y, _ in meetings[i : j + 1]:
1416
g[x].append(y)
1517
g[y].append(x)
1618
s.update([x, y])

solution/2100-2199/2174.Remove All Ones With Row and Column Flips II/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def removeOnes(self, grid: List[List[int]]) -> int:
33
m, n = len(grid), len(grid[0])
4-
state = sum(1 << (i * n + j) for i in range(m)
5-
for j in range(n) if grid[i][j])
4+
state = sum(1 << (i * n + j) for i in range(m) for j in range(n) if grid[i][j])
65
q = deque([state])
76
vis = set([state])
87
ans = 0

0 commit comments

Comments
 (0)