[LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does not matter.
  2. Both m and n are less than 150.

Example:

Given the following 5x5 matrix:

  Pacific ~   ~   ~   ~   ~
       ~  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  *
          *   *   *   *   * Atlantic

Return:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

s

时间: 2024-11-04 12:17:48

[LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流的相关文章

Leetcode: Pacific Atlantic Water Flow

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. Wate

Pacific Atlantic Water Flow 解答

Question Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom ed

417. Pacific Atlantic Water Flow

这题考点在于需要设置两个visited数组 dfs 与bfs不同之处在于使调用符合题意的自己, 而bfs是遍历符合条件的兄弟, 用Queue public class Solution { int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public List<int[]> pacificAtlantic(int[][] matrix) { List<int[]> res = new Array

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

417. 太平洋大西洋水流问题 题目描述 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高到低或者在同等高度上流动. 请找出那些水流既可以流动到"太平洋",又能流动到"大西洋"的陆地单元的坐标. ? 提示: 输出坐标的顺序不重要 m 和 n 都小于150 ? 示例: ? 给定下

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

给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度.“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高到低或者在同等高度上流动. 请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标. 提示: 输出坐标的顺序不重要m 和 n 都小于150 示例: 给定下面的 5x5 矩阵: 太平洋 ~ ~ ~ ~ ~ ~ 1 2 2 3 (5) * ~ 3 2 3 (4) (4) * ~ 2

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,

[LeetCode] [Trapping Rain Water 2012-03-10]

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

LeetCode: Trapping Rain Water [041]

[题目] Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented

LeetCode OJ-- Trapping Rain Water*

https://oj.leetcode.com/problems/trapping-rain-water/ 模拟题,计算出在凹凸处存水量. 对于一个位置 i ,分别计算出它左边的最大值 left (从左扫描一遍), 右边的最大值 right(从右扫描一遍) .找left right中的最小值,如果大于 A[i],则做 min - A[i] 的累加. class Solution { public: int trap(int A[], int n) { if(n<=1) return 0; vec