Skip to content

Commit

Permalink
feat: update python solutions to lc problems
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed May 13, 2022
1 parent 9581b48 commit 4a96974
Show file tree
Hide file tree
Showing 33 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion solution/0400-0499/0490.The Maze/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Solution:
m, n = len(maze), len(maze[0])
q = deque([start])
rs, cs = start
vis = set([(rs, cs)])
vis = {(rs, cs)}
while q:
i, j = q.popleft()
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
Expand Down
2 changes: 1 addition & 1 deletion solution/0400-0499/0490.The Maze/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Solution:
m, n = len(maze), len(maze[0])
q = deque([start])
rs, cs = start
vis = set([(rs, cs)])
vis = {(rs, cs)}
while q:
i, j = q.popleft()
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
Expand Down
2 changes: 1 addition & 1 deletion solution/0400-0499/0490.The Maze/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def hasPath(
m, n = len(maze), len(maze[0])
q = deque([start])
rs, cs = start
vis = set([(rs, cs)])
vis = {(rs, cs)}
while q:
i, j = q.popleft()
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0773.Sliding Puzzle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Solution:
end = "123450"
if start == end:
return 0
vis = set([(start)])
vis = {start}
q = deque([(start)])
ans = 0
while q:
Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0773.Sliding Puzzle/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Solution:
end = "123450"
if start == end:
return 0
vis = set([(start)])
vis = {start}
q = deque([(start)])
ans = 0
while q:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Solution:
dirs = [-1, 0, 1, 0, -1]
ans = 0
mask = (1 << cnt) - 1
vis = set([(start[0], start[1], 0)])
vis = {(*start, 0)}
while q:
for _ in range(len(q)):
i, j, state = q.popleft()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Solution:
dirs = [-1, 0, 1, 0, -1]
ans = 0
mask = (1 << cnt) - 1
vis = set([(start[0], start[1], 0)])
vis = {(*start, 0)}
while q:
for _ in range(len(q)):
i, j, state = q.popleft()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def shortestPathAllKeys(self, grid: List[str]) -> int:
dirs = [-1, 0, 1, 0, -1]
ans = 0
mask = (1 << cnt) - 1
vis = set([(start[0], start[1], 0)])
vis = {(*start, 0)}
while q:
for _ in range(len(q)):
i, j, state = q.popleft()
Expand Down
9 changes: 5 additions & 4 deletions solution/1100-1199/1197.Minimum Knight Moves/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ class Solution:
def minKnightMoves(self, x: int, y: int) -> int:
q = deque([(0, 0)])
ans = 0
vis = set([(0, 0)])
vis = {(0, 0)}
dirs = ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1))
while q:
for _ in range(len(q)):
i, j = q.popleft()
if (i, j) == (x, y):
return ans
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
for a, b in dirs:
c, d = i + a, j + b
if (c, d) not in vis:
vis.add((c, d))
Expand All @@ -82,7 +83,7 @@ class Solution:
return -1
```

双向 BFS
双向 BFS

```python
class Solution:
Expand All @@ -91,7 +92,7 @@ class Solution:
for _ in range(len(q)):
i, j = q.popleft()
step = m1[(i, j)]
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
for a, b in ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)):
x, y = i + a, j + b
if (x, y) in m1:
continue
Expand Down
7 changes: 4 additions & 3 deletions solution/1100-1199/1197.Minimum Knight Moves/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ class Solution:
def minKnightMoves(self, x: int, y: int) -> int:
q = deque([(0, 0)])
ans = 0
vis = set([(0, 0)])
vis = {(0, 0)}
dirs = ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1))
while q:
for _ in range(len(q)):
i, j = q.popleft()
if (i, j) == (x, y):
return ans
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
for a, b in dirs:
c, d = i + a, j + b
if (c, d) not in vis:
vis.add((c, d))
Expand All @@ -72,7 +73,7 @@ class Solution:
for _ in range(len(q)):
i, j = q.popleft()
step = m1[(i, j)]
for a, b in [[-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1]]:
for a, b in ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)):
x, y = i + a, j + b
if (x, y) in m1:
continue
Expand Down
14 changes: 3 additions & 11 deletions solution/1100-1199/1197.Minimum Knight Moves/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ class Solution:
def minKnightMoves(self, x: int, y: int) -> int:
q = deque([(0, 0)])
ans = 0
vis = set([(0, 0)])
vis = {(0, 0)}
dirs = ((-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1))
while q:
for _ in range(len(q)):
i, j = q.popleft()
if (i, j) == (x, y):
return ans
for a, b in [
[-2, 1],
[-1, 2],
[1, 2],
[2, 1],
[2, -1],
[1, -2],
[-1, -2],
[-2, -1],
]:
for a, b in dirs:
c, d = i + a, j + b
if (c, d) not in vis:
vis.add((c, d))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Solution:
n = len(grid)
target = (n * n - 2, n * n - 1)
q = deque([(0, 1)])
vis = set([(0, 1)])
vis = {(0, 1)}
ans = 0
while q:
for _ in range(len(q)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Solution:
n = len(grid)
target = (n * n - 2, n * n - 1)
q = deque([(0, 1)])
vis = set([(0, 1)])
vis = {(0, 1)}
ans = 0
while q:
for _ in range(len(q)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def check(a, b):
n = len(grid)
target = (n * n - 2, n * n - 1)
q = deque([(0, 1)])
vis = set([(0, 1)])
vis = {(0, 1)}
ans = 0
while q:
for _ in range(len(q)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Solution:
state = sum(1 << (i * n + j) for i in range(m)
for j in range(n) if mat[i][j])
q = deque([state])
vis = set([state])
vis = {state}
ans = 0
dirs = [0, -1, 0, 1, 0, 0]
while q:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Solution:
state = sum(1 << (i * n + j) for i in range(m)
for j in range(n) if mat[i][j])
q = deque([state])
vis = set([state])
vis = {state}
ans = 0
dirs = [0, -1, 0, 1, 0, 0]
while q:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def minFlips(self, mat: List[List[int]]) -> int:
m, n = len(mat), len(mat[0])
state = sum(1 << (i * n + j) for i in range(m) for j in range(n) if mat[i][j])
q = deque([state])
vis = set([state])
vis = {state}
ans = 0
dirs = [0, -1, 0, 1, 0, 0]
while q:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Solution:
if k >= m + n - 3:
return m + n - 2
q = deque([(0, 0, k)])
vis = set([(0, 0, k)])
vis = {(0, 0, k)}
ans = 0
while q:
ans += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Solution:
if k >= m + n - 3:
return m + n - 2
q = deque([(0, 0, k)])
vis = set([(0, 0, k)])
vis = {(0, 0, k)}
ans = 0
while q:
ans += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def shortestPath(self, grid: List[List[int]], k: int) -> int:
if k >= m + n - 3:
return m + n - 2
q = deque([(0, 0, k)])
vis = set([(0, 0, k)])
vis = {(0, 0, k)}
ans = 0
while q:
ans += 1
Expand Down
2 changes: 1 addition & 1 deletion solution/1400-1499/1496.Path Crossing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
class Solution:
def isPathCrossing(self, path: str) -> bool:
x = y = 0
vis = set([(x, y)])
vis = {(x, y)}
for c in path:
if c == 'N':
y += 1
Expand Down
2 changes: 1 addition & 1 deletion solution/1400-1499/1496.Path Crossing/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
class Solution:
def isPathCrossing(self, path: str) -> bool:
x = y = 0
vis = set([(x, y)])
vis = {(x, y)}
for c in path:
if c == 'N':
y += 1
Expand Down
2 changes: 1 addition & 1 deletion solution/1400-1499/1496.Path Crossing/Solution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def isPathCrossing(self, path: str) -> bool:
x = y = 0
vis = set([(x, y)])
vis = {(x, y)}
for c in path:
if c == 'N':
y += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Solution:
ans = 0
while i < n:
s = 0
seen = set([0])
seen = {0}
while i < n:
s += nums[i]
if s - target in seen:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Solution:
ans = 0
while i < n:
s = 0
seen = set([0])
seen = {0}
while i < n:
s += nums[i]
if s - target in seen:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def maxNonOverlapping(self, nums: List[int], target: int) -> int:
ans = 0
while i < n:
s = 0
seen = set([0])
seen = {0}
while i < n:
s += nums[i]
if s - target in seen:
Expand Down
2 changes: 1 addition & 1 deletion solution/1600-1699/1631.Path With Minimum Effort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Solution:
while left < right:
mid = (left + right) >> 1
q = deque([(0, 0)])
vis = set([(0, 0)])
vis = {(0, 0)}
while q:
i, j = q.popleft()
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Solution:
while left < right:
mid = (left + right) >> 1
q = deque([(0, 0)])
vis = set([(0, 0)])
vis = {(0, 0)}
while q:
i, j = q.popleft()
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Solution:
return res

q = deque([start])
vis = set([(start)])
vis = {start}
ans = 0
while q:
ans += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Solution:
return res

q = deque([start])
vis = set([(start)])
vis = {start}
ans = 0
while q:
ans += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Solution:
state = sum(1 << (i * n + j) for i in range(m)
for j in range(n) if grid[i][j])
q = deque([state])
vis = set([state])
vis = {state}
ans = 0
while q:
for _ in range(len(q)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Solution:
state = sum(1 << (i * n + j) for i in range(m)
for j in range(n) if grid[i][j])
q = deque([state])
vis = set([state])
vis = {state}
ans = 0
while q:
for _ in range(len(q)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def removeOnes(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
state = sum(1 << (i * n + j) for i in range(m) for j in range(n) if grid[i][j])
q = deque([state])
vis = set([state])
vis = {state}
ans = 0
while q:
for _ in range(len(q)):
Expand Down

0 comments on commit 4a96974

Please sign in to comment.