From d002d8769cfae23d0f5656bf9f36ba10590371d7 Mon Sep 17 00:00:00 2001 From: StriveDD Date: Wed, 15 Mar 2023 20:36:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A41020.=E9=A3=9E=E6=8A=B5?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F.md=E7=9A=84Python=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C0417.=E5=A4=AA=E5=B9=B3=E6=B4=8B?= =?UTF-8?q?=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE=E9=A2=98?= =?UTF-8?q?.md=E7=9A=84Python=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0417.太平洋大西洋水流问题.md | 95 ++++++++++++++- problems/1020.飞地的数量.md | 113 +++++++++++++++++- 2 files changed, 202 insertions(+), 6 deletions(-) diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index 0ec8ebf9..ec229365 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -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 { - diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md index d244e60c..f97678e8 100644 --- a/problems/1020.飞地的数量.md +++ b/problems/1020.飞地的数量.md @@ -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 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 { +