Skip to content

Commit 4a96974

Browse files
committed
feat: update python solutions to lc problems
1 parent 9581b48 commit 4a96974

File tree

33 files changed

+42
-48
lines changed

33 files changed

+42
-48
lines changed

solution/0400-0499/0490.The Maze/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Solution:
103103
m, n = len(maze), len(maze[0])
104104
q = deque([start])
105105
rs, cs = start
106-
vis = set([(rs, cs)])
106+
vis = {(rs, cs)}
107107
while q:
108108
i, j = q.popleft()
109109
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:

solution/0400-0499/0490.The Maze/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Solution:
8787
m, n = len(maze), len(maze[0])
8888
q = deque([start])
8989
rs, cs = start
90-
vis = set([(rs, cs)])
90+
vis = {(rs, cs)}
9191
while q:
9292
i, j = q.popleft()
9393
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:

solution/0400-0499/0490.The Maze/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def hasPath(
55
m, n = len(maze), len(maze[0])
66
q = deque([start])
77
rs, cs = start
8-
vis = set([(rs, cs)])
8+
vis = {(rs, cs)}
99
while q:
1010
i, j = q.popleft()
1111
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:

solution/0700-0799/0773.Sliding Puzzle/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Solution:
121121
end = "123450"
122122
if start == end:
123123
return 0
124-
vis = set([(start)])
124+
vis = {start}
125125
q = deque([(start)])
126126
ans = 0
127127
while q:

solution/0700-0799/0773.Sliding Puzzle/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Solution:
9898
end = "123450"
9999
if start == end:
100100
return 0
101-
vis = set([(start)])
101+
vis = {start}
102102
q = deque([(start)])
103103
ans = 0
104104
while q:

solution/0800-0899/0864.Shortest Path to Get All Keys/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Solution:
8989
dirs = [-1, 0, 1, 0, -1]
9090
ans = 0
9191
mask = (1 << cnt) - 1
92-
vis = set([(start[0], start[1], 0)])
92+
vis = {(*start, 0)}
9393
while q:
9494
for _ in range(len(q)):
9595
i, j, state = q.popleft()

solution/0800-0899/0864.Shortest Path to Get All Keys/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Solution:
7878
dirs = [-1, 0, 1, 0, -1]
7979
ans = 0
8080
mask = (1 << cnt) - 1
81-
vis = set([(start[0], start[1], 0)])
81+
vis = {(*start, 0)}
8282
while q:
8383
for _ in range(len(q)):
8484
i, j, state = q.popleft()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def shortestPathAllKeys(self, grid: List[str]) -> int:
1111
dirs = [-1, 0, 1, 0, -1]
1212
ans = 0
1313
mask = (1 << cnt) - 1
14-
vis = set([(start[0], start[1], 0)])
14+
vis = {(*start, 0)}
1515
while q:
1616
for _ in range(len(q)):
1717
i, j, state = q.popleft()

solution/1100-1199/1197.Minimum Knight Moves/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ class Solution:
6767
def minKnightMoves(self, x: int, y: int) -> int:
6868
q = deque([(0, 0)])
6969
ans = 0
70-
vis = set([(0, 0)])
70+
vis = {(0, 0)}
71+
dirs = ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1))
7172
while q:
7273
for _ in range(len(q)):
7374
i, j = q.popleft()
7475
if (i, j) == (x, y):
7576
return ans
76-
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
77+
for a, b in dirs:
7778
c, d = i + a, j + b
7879
if (c, d) not in vis:
7980
vis.add((c, d))
@@ -82,7 +83,7 @@ class Solution:
8283
return -1
8384
```
8485

85-
双向 BFS
86+
双向 BFS
8687

8788
```python
8889
class Solution:
@@ -91,7 +92,7 @@ class Solution:
9192
for _ in range(len(q)):
9293
i, j = q.popleft()
9394
step = m1[(i, j)]
94-
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
95+
for a, b in ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)):
9596
x, y = i + a, j + b
9697
if (x, y) in m1:
9798
continue

solution/1100-1199/1197.Minimum Knight Moves/README_EN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ class Solution:
4848
def minKnightMoves(self, x: int, y: int) -> int:
4949
q = deque([(0, 0)])
5050
ans = 0
51-
vis = set([(0, 0)])
51+
vis = {(0, 0)}
52+
dirs = ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1))
5253
while q:
5354
for _ in range(len(q)):
5455
i, j = q.popleft()
5556
if (i, j) == (x, y):
5657
return ans
57-
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
58+
for a, b in dirs:
5859
c, d = i + a, j + b
5960
if (c, d) not in vis:
6061
vis.add((c, d))
@@ -72,7 +73,7 @@ class Solution:
7273
for _ in range(len(q)):
7374
i, j = q.popleft()
7475
step = m1[(i, j)]
75-
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
76+
for a, b in ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)):
7677
x, y = i + a, j + b
7778
if (x, y) in m1:
7879
continue

0 commit comments

Comments
 (0)