417. 太平洋大西洋水流问题

给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。

规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。

请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。

提示:

输出坐标的顺序不重要

m 和 n 都小于150

示例:

给定下面的 5x5 矩阵:

  太平洋 ~   ~   ~   ~   ~
~ 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 *
* * * * * 大西洋

返回:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (上图中带括号的单元).

class Solution {
private int row, col;
private int[][] grid;
private List<List<Integer>> result = new ArrayList<>(); public List<List<Integer>> pacificAtlantic(int[][] matrix) {
row = matrix.length;
if (row == 0) {
return result;
}
col = matrix[0].length;
grid = new int[row][col];
for (int i = 0; i < row; i++) {
helper(matrix, i, 0, 1);
}
for (int j = 0; j < col; j++) {
helper(matrix, 0, j, 1);
}
for (int i = 0; i < row; i++) {
helper(matrix, i, col - 1, 2);
}
for (int j = 0; j < col; j++) {
helper(matrix, row - 1, j, 2);
}
return result;
} private void helper(int[][] matrix, int i, int j, int v) {
if (grid[i][j] == v || grid[i][j] == 3) {
return;
}
grid[i][j] += v;
if (grid[i][j] == 3) {
List<Integer> temp = new ArrayList<>();
temp.add(i);
temp.add(j);
result.add(temp);
}
if (i != 0 && matrix[i - 1][j] >= matrix[i][j]) {
helper(matrix, i - 1, j, v);
}
if (j != 0 && matrix[i][j - 1] >= matrix[i][j]) {
helper(matrix, i, j - 1, v);
}
if (i != row - 1 && matrix[i + 1][j] >= matrix[i][j]) {
helper(matrix, i + 1, j, v);
}
if (j != col - 1 && matrix[i][j + 1] >= matrix[i][j]) {
helper(matrix, i, j + 1, v);
}
} }

Java实现 LeetCode 417 太平洋大西洋水流问题的更多相关文章

  1. Leetcode 417.太平洋大西洋水流问题

    太平洋大西洋水流问题 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的右边界和下 ...

  2. [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 ...

  3. [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 ...

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

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

  5. [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 ...

  6. DFS(深度优先搜索遍历有向图)-03-有向图-太平洋大西洋水流问题

    给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度.“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高 ...

  7. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. 【matlab 基础篇 02】基础知识一键扫盲,看完即可无障碍编程(超详细+图文并茂)

    博主快速入门matlab,系统地整理一遍,如何你和我一样是一个新手,那么此文很适合你: 本人能力有限,文中难免有错误和纰漏之处,请大佬们不吝赐教 创作不易,如果本文帮到了您: 请帮忙点个赞

  2. 【系列】Python编程思想(1):Python简介与开发环境搭建

    李宁老师的 开始学习.   本系列文章深入介绍了Python的各种技术,堪称是目前最全的Python教程.主要目的是让读者可以了解Python的各种核心技术,包括各种Python函数库.本教程使用Py ...

  3. windows电脑关闭自动更新的方法

    第一步.打开我的电脑,点击此电脑,然后点击管理 第二步.在计算机管理(本地)里面找到服务和应用程序,点击进入 第三步.进去,点击服务 第四步.往下滑,找到windows update,点击进入 第五步 ...

  4. java经典问题 byte b=1、b=b+1、b+=1

    直接问题: 首先 byte的范围 [-128,127] byte 类型可以自动转为int类型 int类型不能自动转为byte类型. 超过byte的范围,就会变成int类型了 byte b=1:正确, ...

  5. masonry中的make,remake,update

    - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; ...

  6. 关于QQ可以发消息但是网页刷不出来问题

    相信很多人都遇到过这个问题,明明可以登陆QQ,但是网页就是打不开,而且这种情况经常伴有网卡图标显示叹号的情况.笔者这里就教你一个方法,保证好用. 首先,在开始菜单输入cmd,在命令符模式下点击右键选择 ...

  7. 10大Web漏洞扫描工具

    Web scan tool 推荐10大Web漏洞扫描程序 Nikto 这是一个开源的Web服务器扫描程序,它可以对Web服务器的多种项目(包括3500个潜在的危险文件/CGI,以及超过900个服务器版 ...

  8. VST的安装

    对需要使用VST的用户,你可以到http://www.soft-gems.net/去免费下载没有使用限制.没有广告的VST.包括例子程序以及说明文档也可以下载到,下载完成后,就是安装,以前版本的VST ...

  9. ql的python学习之路-day5

    文件操作 文件操作流程: 1.打开文件得到文件句柄并赋值变量 2.通过句柄对文件进行操作 3.关闭文件 打开的只是储存在计算机里的文件对象,必须赋值一个变量才能操作,变量通常用f表示,赋值f的文件对象 ...

  10. IDEA图标大全

      IntelliJ IDEA 2019.3版本以来各种小图标的含义 Common Icon Description Class Abstract class Groovy class Annotat ...