原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/

题目:

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does not matter.
  2. Both m and n are less than 150.

Example:

Given the following 5x5 matrix:

  Pacific ~   ~   ~   ~   ~
~ 1 2 2 3 (5) *
~ 3 2 3 (4) (4) *
~ 2 4 (5) 3 1 *
~ (6) (7) 1 4 5 *
~ (5) 1 1 2 4 *
* * * * * Atlantic Return: [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

题解:

能流淌到左边界 和 上边界的就是能流到Pacific. 那么反过来左边界 和 上边界的点反过来, 往高走 能到达的点就都是能流到Pacific的点.

从左边界和上边界所有点BFS 能visit到的点都是能流到Pacific的点.

同理, 从右边界和下边界所有点BFS能visit道德点都是能留到Atlantic的点. 若有交集 就是 能流到both Pacific and Atlantic的点.

Time Complexity: O(m*n). m = matrix.length. n = matrix[0].length.

Space: O(m*n).

AC Java:

 class Solution {
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int []> res = new ArrayList<int []>();
if(matrix == null || matrix.length == 0|| matrix[0].length == 0){
return res;
} int m = matrix.length;
int n = matrix[0].length;
int [][] dirs = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; // 左上BFS
boolean [][] visitedUpLeft = new boolean[m][n];
LinkedList<int []> que = new LinkedList<int []>();
for(int i = 0; i<m; i++){
if(!visitedUpLeft[i][0]){
visitedUpLeft[i][0] = true;
que.add(new int[]{i, 0});
}
} for(int j = 0; j<n; j++){
if(!visitedUpLeft[0][j]){
visitedUpLeft[0][j] = true;
que.add(new int[]{0, j});
}
} while(!que.isEmpty()){
int [] cur = que.poll();
for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || visitedUpLeft[x][y] || matrix[x][y]<matrix[cur[0]][cur[1]]){
continue;
} visitedUpLeft[x][y] = true;
que.add(new int[]{x, y});
}
} // 右下BFS
boolean [][] visitedLowRight = new boolean[m][n];
for(int i = 0; i<m; i++){
if(!visitedLowRight[i][n-1]){
visitedLowRight[i][n-1] = true;
que.add(new int[]{i, n-1});
}
} for(int j = 0; j<n; j++){
if(!visitedLowRight[m-1][j]){
visitedLowRight[m-1][j] = true;
que.add(new int[]{m-1, j});
}
} while(!que.isEmpty()){
int [] cur = que.poll();
if(visitedUpLeft[cur[0]][cur[1]]){
res.add(cur);
} for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || visitedLowRight[x][y] || matrix[x][y]<matrix[cur[0]][cur[1]]){
continue;
} visitedLowRight[x][y] = true;
que.add(new int[]{x, y});
}
} return res;
}
}

LeetCode 417. Pacific Atlantic Water Flow的更多相关文章

  1. [LeetCode] 417. Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  2. 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...

  3. 417 Pacific Atlantic Water Flow 太平洋大西洋水流

    详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...

  4. 417. Pacific Atlantic Water Flow

    正常做的,用了645MS..感觉DFS的时候剪枝有问题.. 为了剪枝可能需要标记一个点的4种情况: 1:滨临大西洋,所有太平洋来的点可以通过: 2:濒临太平洋,所有大西洋来的点可以通过: 3:都不濒临 ...

  5. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  6. Leetcode: Pacific Atlantic Water Flow

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  7. [LeetCode] Pacific Atlantic Water Flow 题解

    题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...

  8. [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  9. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

随机推荐

  1. hand first python 选读(1)

    列表(list) 基本操作 比如说我要整理一个近期热映的电影列表: movies = ["venom", "My Neighbor Totor", " ...

  2. getline

    istream& istream::getline(char*, streamsize,char= '\n'); 函数getline与get的区别在于,函数get当遇到分隔符后,停止获取,并将 ...

  3. PHP返回32位与16位的md5加密值

    字符串“123456”,经过md5算法加密之后是 32位: e10adc3949ba59abbe56e057f20f883e16位: 49ba59abbe56e057 PHP自带的 md5() 函数, ...

  4. JavaScript深拷贝—我遇到的应用场景

    简述 深拷贝即拷贝实例,其作用是为了不影响拷贝后的数组对起原数组造成影响.这时我们就需要进行深拷贝.(JavaScript的继承) 我遇到的应用场景 我是在用vue的element-ui做项目的时候遇 ...

  5. Spring Boot Logback几种日志详解

    日志对于应用程序来说是非常重要的,Spring框架本身集成了不少其他工具,我们自身的应用也会使用到第三方库,所以我们推荐在Spring应用中使用SLF4J/Logback来记录日志. SLF4J与Lo ...

  6. commons-fileupload实现上传进度条的显示

    本文将使用   apache fileupload   ,spring MVC   jquery 实现一个带进度条的多文件上传, 由于fileupload 的局限,暂不能实现每个上传文件都显示进度条, ...

  7. dell n2024交换机配置

    1.putty串口设置 9600 8 1 None None 2.连接 3.console>en 4.带内管理IP地址 console(config)#username admin passwo ...

  8. C#常用函数与方法集合

    1.DateTime   数字型         System.DateTime currentTime=new System.DateTime();    1.1 取当前年月日时分秒         ...

  9. bzoj2547

    题解: 二分+宽搜+KM 显然答案不能太大 然后二分一下 代码: #include<bits/stdc++.h> ,M=; using namespace std; ]={-,,,},dy ...

  10. RabbitMQ(3) Java客户端使用

    RabbitMQ针对不同的开发语言(java,python,c/++,Go等等),提供了丰富对客户端,方便使用.就Java而言,可供使用的客户端有RabbitMQ Java client. Rabbi ...