1.题目
2.解法
①二维数组+dfs
class Solution {
private int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private int row, col;
public List<List<Integer>> pacificAtlantic(int[][] heights) {
if(heights.length == 0 || heights[0].length == 0) return new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
row = heights.length;
col = heights[0].length;
boolean[][] tai = new boolean[row][col];
boolean[][] big = new boolean[row][col];
for(int i = 0; i < row; i++){
dfs(i, 0, heights, tai);
dfs(i, col - 1, heights, big);
}
for(int j = 0; j < col; j++){
dfs(0, j, heights, tai);
dfs(row - 1, j, heights, big);
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(tai[i][j] && big[i][j]) res.add(Arrays.asList(i, j));
}
}
return res;
}
private void dfs(int r, int c, int[][] heights, boolean[][] canReach){
if(canReach[r][c]) return;
canReach[r][c] = true;
for(int[] d : directions){
int nextR = r + d[0];
int nextC = c + d[1];
if(nextR < 0 || nextR >= row || nextC < 0 || nextC >= col || heights[nextR][nextC] < heights[r][c]){
continue;
}
dfs(nextR, nextC, heights, canReach);
}
}
}