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. 【Hadoop离线基础总结】HDFS入门介绍

    HDFS入门介绍 概述 HDFS全称为Hadoop Distribute File System,也就是Hadoop分布式文件系统,是Hadoop的核心组件之一. 分布式文件系统是横跨在多台计算机上的 ...

  2. 借助DEM生成高精度SketchUp地形,地形分析如此简单

    SketchUp因其自身友好的界面和强大的功能,已经成为建筑规划设计常用工具.无论是摩天大楼还是独栋别墅,小区规划还方案推敲和方案表现上都显示出了不错的实力. 然而SketchUp异形建模能力对比3d ...

  3. qt获取指定目录下符合条件的文件路径

    1)设置名称过滤器 QDir * dir = new QDir(路径); QStringList filter; Filter << QStringLiteral(“筛选的文件条件,如.x ...

  4. 【hdu1024】简单dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 最大m字段和,题目就不多说了,经典dp 这题坑爹...首先不说明m的范围(n<=1000000),还 ...

  5. PK,FK, UK,DF, CK

    PK 主键 constraint primary key FK 主外键关系 constraint foreign references UK 唯一约束 constraint unique key DF ...

  6. Spring全家桶之springMVC(一)

    Spring MVC简介和第一个spring MVC程序 Spring MVC是目前企业中使用较多的一个MVC框架,被很多业内人士认为是一个教科书级别的MVC表现层框架,Spring MVC是大名鼎鼎 ...

  7. java读取文件内容常见几种方式

    ①随机读取文件内容 ②以行为单位读取文件,常用于读面向行的格式化文件 ③以字符为单位读取文件,常用于读文本,数字等类型的文件 ④以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件 pa ...

  8. 「雕爷学编程」Arduino动手做(8)——湿度传感器模块

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  9. Notification API,为你的网页添加桌面通知推送

    Notification 是什么 MDN: Notifications API 的 Notification 接口用于配置和向用户显示桌面通知.这些通知的外观和特定功能因平台而异,但通常它们提供了一种 ...

  10. Jenkins-Sonar集成配置及注意点

    首先说说关于Jenkins集成Sonar的相关配置:我jenkins与Sonar不在同一个服务器上! 先现在 SonarQube Scanner 插件. SonarQube Servers:系统配置 ...