原题链接在这里: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;
}
}

最新文章

  1. codewars 随手记
  2. 维护MMO项目的随想
  3. POJ 3683 Priest John&#39;s Busiest Day(2-SAT 并输出解)
  4. Idol之坑
  5. Sublime Text 介绍、用法、插件等
  6. shell进行mysql统计
  7. BFC探秘
  8. 大数据笔记10:大数据之Hadoop的MapReduce的原理
  9. ASP.Net引用类库出现问题 二
  10. windows下配置PHP+Nginx+MySQL完整流程(转)
  11. 访问WEB-INF下的jsp/html
  12. VOD, TVOD, SVOD FVOD的区别(转)
  13. appium如何切换Native和WebView
  14. ubuntu11.04启动 及虚拟文件系统
  15. Uncaught ReferenceError: jQuery is not defined
  16. win7系统搭建FTP服务器
  17. rabbitmq.config配置参数详解
  18. Ecplise 快捷键笔记
  19. (网页)jQuery的时间datetime控件在AngularJs中使用实例
  20. C# 实现Bresenham算法(vs2010)

热门文章

  1. 【Python】解决Python脚本 在cmd命令行窗口运行时,中文乱码问题
  2. zabbix自动化运维学习笔记(服务器安装)
  3. 设置ListBox的Item的样式
  4. Android ---------高德卫星地图绘制多个点和点的点击事件自定义弹窗
  5. Spring框架中,在工具类或者普通Java类中调用service或dao
  6. 十四 web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码
  7. centos7&amp;redhat 之 firewalld 详细介绍配置
  8. 双系统下ubuntu不能访问658GB卷,磁盘挂载失败。
  9. 一款连接SqlServer的数据库工具
  10. C#笔记 -- 协变、逆变