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. [csu1605]数独(精确覆盖问题)

    题意 :给定数独的某些初始值,规定每个格子的得分,求得分最大的数独的解. 思路:这是某年的noip的原题,高中时就写过,位运算也就是那个时候学会的--.这题明显是暴搜,但是需要注意两点,一是需要加一些 ...

  2. Spring全家桶之spring boot(四)

    spring boot拦截器.过滤器.servlet和健康检查机制  spring boot拦截器 spring boot配置拦截器与原来大致相同,只是需要在拦截器的配置类上添加@Configurat ...

  3. mysql小白系列_04 binlog(未完)

    mysql打开.查看.清理binlog 1.开启日志 log_bin=/var/lib/mysql/mysql-bin mysql> show variables like '%log_bin% ...

  4. BZOJ1040 基环森林 找环+基础树形DP

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4752  Solved: 1831[Submit][Status ...

  5. BZOJ1022

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2701  Solved: 1721[Submit] ...

  6. 【git】git 常用命令(含删除文件)

    Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v ...

  7. 公众号使用微信sdk的正确姿势

    当我们做微信登录授权,微信公众号的分享,微信的h5支付等等等等的时候难免会用到微信sdk,当我们用react或vue做的spa应用,直接引入后会发现,在按安卓上可以正常调试,而ios上一直报签名错误( ...

  8. vue 配置移动单位转换插件 postcss-px-to-viewport

    1.先安装插件 npm install postcss-px-to-viewport --save-dev 2.在文件根目录下添加 postcss.config.js 文件 module.export ...

  9. PHP 操作结果集对象方法

    <?php header('Content-type:text/html;charset=utf-8'); //建立 或者 关闭mysql服务器 @符号用于屏蔽错误信息 $link=@mysql ...

  10. 搭建Istio基础环境

    需求 搭建istio基础环境(基于1.5.1版本) 安装步骤 在安装 Istio 之前,需要一个运行着 Kubernetes 的环境,安装步骤可以参考前面的文章 下载istio,然后解压,然后将 is ...