原题链接在这里: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. JavaScript权威指南--客户端存储

    客户端存储web应用允许使用浏览器提供的API实现将数据存储在用户电脑上. 客户端存储遵循“同源策略”,因此不同站点的页面是无法读取对于存储的数据.而同一站点的不同的页面之间是可以互相共享存储数据的. ...

  2. 新东方雅思词汇---10.1、(a)esthet

    新东方雅思词汇---10.1.(a)esthet 一.总结 一句话总结: 感觉 aesthetic 英 [i:sˈθetɪk]  美 [esˈθetɪk; ɛsˈθɛtɪk]  adj. 美的:美学的 ...

  3. JSP 异常处理

    JSP 异常处理 当编写JSP程序的时候,程序员可能会遗漏一些BUG,这些BUG可能会出现在程序的任何地方.JSP代码中通常有以下几类异常: 检查型异常:检查型异常就是一个典型的用户错误或者一个程序员 ...

  4. fio测试freenas共享的iscsi磁盘性能

    4k随机读iops: fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randread -filename=/dev/sdb -iodepth=32 ...

  5. Metasploit 使用简介

    Metasploit Framework 是非常优秀的开源渗透测试框架,像我这样的菜鸟刚刚听说,于是花时间好好研究了一下,整理了一下学习笔记,贴出来和大家一起交流.第一次写文章又不足的地方大家多多指点 ...

  6. webapi返回不带引号的字符串,解决自动加双引号的问题

    返回类型改为HttpResponseMessage类型 [Route("api/TestControllers/test")] [HttpGet] public HttpRespo ...

  7. 【实用工具】google访问不了比较好的解决办法

    一.直接添加ip,74.125.128.(31~167)都可以,不过不稳定 二.不使用.hk的后缀名,使用.tw,co.jp等均可(同IE用户) 三.下载这个脚本,进行安装(右上角点击“install ...

  8. Spring写配置文件时不提示怎么办?

    想要编写xml文件时代码提示,其实只要配置了相应的xsd文件即可,xsd文件也就是xml文件的一个约束.就是决定了你xml文件中可以写哪些东西. xsd文件是xml文件的元数据文件. 以bean标签为 ...

  9. C# Http方式下载文件到本地类

    直接上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  10. Spring整合CXF发布及调用WebService

    这几天终于把webService搞定,下面给大家分享一下发布webService和调用webService的方法 添加jar包 (官方下载地址:http://cxf.apache.org/downlo ...