Java实现 LeetCode 417 太平洋大西洋水流问题
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 太平洋大西洋水流问题的更多相关文章
- Leetcode 417.太平洋大西洋水流问题
太平洋大西洋水流问题 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的右边界和下 ...
- [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 ...
- [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 ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- [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 ...
- DFS(深度优先搜索遍历有向图)-03-有向图-太平洋大西洋水流问题
给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度.“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高 ...
- 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 ...
- 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. ...
- 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 ...
随机推荐
- GP1UM26(78)1RK远程红外遥控接收IC数据手册学习
1.Features 该系列IC具有多种BMP带通频率可供选择,典型的GP1UM261RK带通频率为38KHz,内部的前置放大器等放大电路工作频率均为38KHz. Compact紧凑型,体积小 2.i ...
- 在Qsys中创建用户自定义IP
在SOC FPGA的设计中,必须使用Qsys软件才能将ARM和FPGA之间的接口引入到FPGA设计中.为了设计上的方便,客户经常希望将Qsys中的一些接口信号引入到FPGA顶层设计文件中.本文以Ava ...
- navicat 远程链接Mysql问题
mysql服务器需要配置 给远程访问配置权限:注意自己的用户名和密码 使用select host, user, password from mysql.user;查看是否配置好相应的用户和密码 nav ...
- docker build报错
docker build 遇到问题 "can not stat ... APPData\Local\Application Data" 解决方法:
- equals(), "== ",hashcode() 详细解释
Object 通用方法容易混淆的定义 先搞清楚各自的定义 "==" 用来判断 相等 equals() 用来判断 等价 hashcode() 用来返回散列值 "==&quo ...
- linux下安装gmp遇到 configure:error:no usable m4 in$path or /user/5bin解决方案
安装过程中遇到如下报错: 上面的报错是因为你没有安装m4,安装m4就可以了:以下两种命令人选其一: #yum install m4 或 #apt-get install m4 ps:如果遇到权限问题就 ...
- ORACLE 统计文件后缀SQL
ORACLE 统计文件后缀SQL查询语句 select lower(substr(file_name,instr(file_name, '.', -1),length(file_name))),cou ...
- Puppeteer笔记(一):Puppeteer简介
一.Puppeteer简介 Puppeteer是NPM库,它提供了NodeJS高级API来控制Chrome.Puppeteer 默认以无头(无界面)方式运行,但也可以配置为运行有界面的Chrome. ...
- 09 基于模块wsgiref版web框架
09 基于模块wsgiref版web框架 模块引入 真实开发中的python web程序,一般会分为两部分: 服务器程序:负责对socket服务器进行封装,并在请求到来时,对请求的各种数据 ...
- php连接数据库 需要下载adodb
<?include('adodb/ADOdb.inc.php'); # 加载ADODB$conn = &ADONewConnection('odbc_mssql'); # 建立一个连结$ ...