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. [hdu4301]DP

    题意:给一个2*n的矩形块,求把它分成k个连通块的方法数.(有公共边即视为联通) 思路:由于宽度只有2,于是很容易设计状态使问题满足阶段性以及无后效性.具体来说,令dp[i][j][0]和dp[i][ ...

  2. OpenStack黄金十年:我与OpenStack的故事

    导读:从2010年到2020年,OpenStack项目整整走过了十个春夏秋冬.不管是OpenStack基金会,还是积极参与OpenStack社区的厂商.企业乃至开发者,想必都有肺腑之言想对OpenSt ...

  3. REST模式中HTTP请求方法

    一直在测试REST模式的WEB SERVICE接口,客户端的HTTP的请求方式一般分为四种:GET.POST.PUT.DELETE,这四种请求方式有什么不同呢.简单的说,GET就是获取资源,POST就 ...

  4. NOI Online #2 赛后题解

    color 题意 \(\;\) 给定\(p_1,p_2\),要求\(p_1\)的倍数格子填红色,\(p_2\)的倍数格子填蓝色,既是\(p_1\)又是\(p_2\)倍数的格子颜色任选.求是否存在一种填 ...

  5. mysql批量修改

    update odr_order_base INNER JOIN (select merchant_id,order_base_id from odr_order_commodity) b on od ...

  6. Python单元测试框架:pytest

    (一)介绍 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1.简单灵活,容易上手: 2.支持参数化: 3.能够支持简单的单元测试和复杂的功能测试,还可以用来做sele ...

  7. JQuery踩过的坑,遇到就记下

    1 乱用选择器 坑人指数:200 JQuery选择器调用代价很大,反复调用效率更低.应采用缓存对象的方法或采用链式调用的方式. //错误的写法 $("#button").click ...

  8. 一文解读C# 动态拦截第三方进程中的方法函数(外挂必备)

    一.前言 由于项目需要,最近研究了一下跨进程通讯改写第三方程序中的方法(运行中),把自己程序中的目标方法直接覆盖第三方程序中的方法函数:一直没有头绪,通过搜索引擎找了一大堆解决方案,资料甚是稀少,最后 ...

  9. 如何用尾插法建立双链表(C语言,非循环)

    如何用尾插法建立双链表 其实本来是想完成汪队给的链表快排的作业,但是我写完建立双链表以后就12点了龟龟,明天还要早起QAQ,我菜死了 一,为啥要有双链表 先说单链表吧单链表长这样 他的一个结点结构就是 ...

  10. 微信小程序var和let以及const有什么区别

    微信小程序var和let以及const的区别: 在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循 ...