原题链接在这里: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. Java subList的使用

    1. 在看<阿里巴巴java编程手册的时候>有如下强制约束 顺便学了一下subList. java.util.List中有一个subList方法,用来返回一个list的一部分的视图. Li ...

  2. Spring事务源码分析总结

    Spring事务是我们日常工作中经常使用的一项技术,Spring提供了编程.注解.aop切面三种方式供我们使用Spring事务,其中编程式事务因为对代码入侵较大所以不被推荐使用,注解和aop切面的方式 ...

  3. Spring3.0 核心jar包详解

    org.springframework.aop  包含在应用中使用Spring的AOP特性时所需的类. org.springframework.asm   Spring独立的ASM程序, Spring ...

  4. Linux IPv6 地址配置

    添加IPV6地址ip -6 addr add <ipv6address>/<prefixlength> dev <interface>ip -6 addr add ...

  5. JWT(JSON Web Token) Java与.Net简单编码实现

    参考 JWT(JSON WEB TOKENS)-一种无状态的认证机制 基于Token的WEB后台认证机制 各种语言版本的基于HMAC-SHA256的base64加密 Java与.Net实现实现 // ...

  6. day29 主机管理-堡垒机2-原生ssh会话记录

    day29课堂代码:https://github.com/liyongsan/git_class/tree/master/day29 课堂笔记: 通过原生Ssh 记录会话1. 在我们自己的堡垒机交互脚 ...

  7. NPOI自定义单元格背景颜色

    经常在NPOI群里聊天时发现有人在问NPOI设置单元格背景颜色的问题,而Tony Qu大神的博客里没有相关教程,刚好最近在做项目时研究了一下这一块,在这里总结一下. 在NPOI中默认的颜色类是HSSF ...

  8. SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1

    5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...

  9. hdu 1536&&1944 S-Nim sg函数 难度:0

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  10. UML_01_画图工具

    一.推荐工具 1.processon 在线画图,类型丰富 www.processon.com 2.StarUML staruml.io 破解方法 StarUML 3.0.2 (Crack + Keyg ...