提交1020.飞抵的数量.md的Python版本代码,0417.太平洋大西洋水流问题.md的Python版本代码

This commit is contained in:
StriveDD
2023-03-15 20:36:34 +08:00
parent d1eccfba1a
commit d002d8769c
2 changed files with 202 additions and 6 deletions

View File

@ -356,6 +356,100 @@ class Solution {
}
```
### Python
深度优先遍历
```Python3
class Solution:
def __init__(self):
self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向
# heights题目给定的二维数组 row当前位置的行号 col当前位置的列号
# sign记录是哪一条河两条河中可以一个为 0一个为 1
# visited记录这个位置可以到哪条河
def dfs(self, heights: List[List[int]], row: int, col: int, sign: int, visited: List[List[List[int]]]):
for current in self.position:
curRow, curCol = row + current[0], col + current[1]
# 索引下标越界
if curRow < 0 or curRow >= len(heights) or curCol < 0 or curCol >= len(heights[0]): continue
# 不满足条件或者已经被访问过
if heights[curRow][curCol] < heights[row][col] or visited[curRow][curCol][sign]: continue
visited[curRow][curCol][sign] = True
self.dfs(heights, curRow, curCol, sign, visited)
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
rowSize, colSize = len(heights), len(heights[0])
# visited 记录 [row, col] 位置是否可以到某条河,可以为 true反之为 false
# 假设太平洋的标记为 1大西洋为 0
# ans 用来保存满足条件的答案
ans, visited = [], [[[False for _ in range(2)] for _ in range(colSize)] for _ in range(rowSize)]
for row in range(rowSize):
visited[row][0][1] = True
visited[row][colSize - 1][0] = True
self.dfs(heights, row, 0, 1, visited)
self.dfs(heights, row, colSize - 1, 0, visited)
for col in range(0, colSize):
visited[0][col][1] = True
visited[rowSize - 1][col][0] = True
self.dfs(heights, 0, col, 1, visited)
self.dfs(heights, rowSize - 1, col, 0, visited)
for row in range(rowSize):
for col in range(colSize):
# 如果该位置即可以到太平洋又可以到大西洋,就放入答案数组
if visited[row][col][0] and visited[row][col][1]:
ans.append([row, col])
return ans
```
广度优先遍历
```Python3
class Solution:
def __init__(self):
self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]]
# heights题目给定的二维数组visited记录这个位置可以到哪条河
def bfs(self, heights: List[List[int]], queue: deque, visited: List[List[List[int]]]):
while queue:
curPos = queue.popleft()
for current in self.position:
row, col, sign = curPos[0] + current[0], curPos[1] + current[1], curPos[2]
# 越界
if row < 0 or row >= len(heights) or col < 0 or col >= len(heights[0]): continue
# 不满足条件或已经访问过
if heights[row][col] < heights[curPos[0]][curPos[1]] or visited[row][col][sign]: continue
visited[row][col][sign] = True
queue.append([row, col, sign])
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
rowSize, colSize = len(heights), len(heights[0])
# visited 记录 [row, col] 位置是否可以到某条河,可以为 true反之为 false
# 假设太平洋的标记为 1大西洋为 0
# ans 用来保存满足条件的答案
ans, visited = [], [[[False for _ in range(2)] for _ in range(colSize)] for _ in range(rowSize)]
# 队列,保存的数据为 [行号, 列号, 标记]
# 假设太平洋的标记为 1大西洋为 0
queue = deque()
for row in range(rowSize):
visited[row][0][1] = True
visited[row][colSize - 1][0] = True
queue.append([row, 0, 1])
queue.append([row, colSize - 1, 0])
for col in range(0, colSize):
visited[0][col][1] = True
visited[rowSize - 1][col][0] = True
queue.append([0, col, 1])
queue.append([rowSize - 1, col, 0])
self.bfs(heights, queue, visited) # 广度优先遍历
for row in range(rowSize):
for col in range(colSize):
# 如果该位置即可以到太平洋又可以到大西洋,就放入答案数组
if visited[row][col][0] and visited[row][col][1]:
ans.append([row, col])
return ans
```
@ -363,4 +457,3 @@ class Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -170,7 +170,8 @@ class Solution {
public int numEnclaves(int[][] grid) {
int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案
boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组
// 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 true反之为 false
boolean[][] visited = new boolean[rowSize][colSize];
// 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历
for (int row = 0; row < rowSize; row++) {
if (grid[row][0] == 1 && !visited[row][0]) {
@ -230,9 +231,10 @@ class Solution {
public int numEnclaves(int[][] grid) {
int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案
boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组
// 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 true反之为 false
boolean[][] visited = new boolean[rowSize][colSize];
Queue<int[]> queue = new ArrayDeque<>();
// 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历
// 搜索左侧边界和右侧边界查找 1 存入队列
for (int row = 0; row < rowSize; row++) {
if (grid[row][0] == 1) {
visited[row][0] = true;
@ -243,7 +245,7 @@ class Solution {
queue.add(new int[]{row, colSize - 1});
}
}
// 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了
// 搜索上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了
for (int col = 1; col < colSize - 1; col++) {
if (grid[0][col] == 1) {
visited[0][col] = true;
@ -254,7 +256,7 @@ class Solution {
queue.add(new int[]{rowSize - 1, col});
}
}
bfs(grid, queue, visited);
bfs(grid, queue, visited); // 广度优先遍历
// 查找没有标记过的 1记录到 ans 中
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < colSize; col++) {
@ -266,6 +268,106 @@ class Solution {
}
```
### Python
深度优先遍历
```Python3
class Solution:
def __init__(self):
self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向
# 深度优先遍历,把可以通向边缘部分的 1 全部标记成 true
def dfs(self, grid: List[List[int]], row: int, col: int, visited: List[List[bool]]) -> None:
for current in self.position:
newRow, newCol = row + current[0], col + current[1]
# 索引下标越界
if newRow < 0 or newRow >= len(grid) or newCol < 0 or newCol >= len(grid[0]):
continue
# 当前位置值不是 1 或者已经被访问过了
if grid[newRow][newCol] == 0 or visited[newRow][newCol]: continue
visited[newRow][newCol] = True
self.dfs(grid, newRow, newCol, visited)
def numEnclaves(self, grid: List[List[int]]) -> int:
rowSize, colSize, ans = len(grid), len(grid[0]), 0
# 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 True反之为 False
visited = [[False for _ in range(colSize)] for _ in range(rowSize)]
# 搜索左边界和右边界,对值为 1 的位置进行深度优先遍历
for row in range(rowSize):
if grid[row][0] == 1:
visited[row][0] = True
self.dfs(grid, row, 0, visited)
if grid[row][colSize - 1] == 1:
visited[row][colSize - 1] = True
self.dfs(grid, row, colSize - 1, visited)
# 搜索上边界和下边界,对值为 1 的位置进行深度优先遍历,但是四个角不需要,因为上面遍历过了
for col in range(1, colSize - 1):
if grid[0][col] == 1:
visited[0][col] = True
self.dfs(grid, 0, col, visited)
if grid[rowSize - 1][col] == 1:
visited[rowSize - 1][col] = True
self.dfs(grid, rowSize - 1, col, visited)
# 找出矩阵中值为 1 但是没有被标记过的位置,记录答案
for row in range(rowSize):
for col in range(colSize):
if grid[row][col] == 1 and not visited[row][col]:
ans += 1
return ans
```
广度优先遍历
```Python3
class Solution:
def __init__(self):
self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向
# 广度优先遍历,把可以通向边缘部分的 1 全部标记成 true
def bfs(self, grid: List[List[int]], queue: deque, visited: List[List[bool]]) -> None:
while queue:
curPos = queue.popleft()
for current in self.position:
row, col = curPos[0] + current[0], curPos[1] + current[1]
# 索引下标越界
if row < 0 or row >= len(grid) or col < 0 or col >= len(grid[0]): continue
# 当前位置值不是 1 或者已经被访问过了
if grid[row][col] == 0 or visited[row][col]: continue
visited[row][col] = True
queue.append([row, col])
def numEnclaves(self, grid: List[List[int]]) -> int:
rowSize, colSize, ans = len(grid), len(grid[0]), 0
# 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 True反之为 False
visited = [[False for _ in range(colSize)] for _ in range(rowSize)]
queue = deque() # 队列
# 搜索左侧边界和右侧边界查找 1 存入队列
for row in range(rowSize):
if grid[row][0] == 1:
visited[row][0] = True
queue.append([row, 0])
if grid[row][colSize - 1] == 1:
visited[row][colSize - 1] = True
queue.append([row, colSize - 1])
# 搜索上边界和下边界查找 1 存入队列,但是四个角不用遍历,因为上面已经遍历到了
for col in range(1, colSize - 1):
if grid[0][col] == 1:
visited[0][col] = True
queue.append([0, col])
if grid[rowSize - 1][col] == 1:
visited[rowSize - 1][col] = True
queue.append([rowSize - 1, col])
self.bfs(grid, queue, visited) # 广度优先遍历
# 找出矩阵中值为 1 但是没有被标记过的位置,记录答案
for row in range(rowSize):
for col in range(colSize):
if grid[row][col] == 1 and not visited[row][col]:
ans += 1
return ans
```
## 类似题目
@ -277,3 +379,4 @@ class Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>