leetcode 417.Pacific Atlantic Water Flow(大西洋和太平洋的水流)

leetcode 417. 太平洋大西洋水流问题 https://leetcode-cn.com/problems/pacific-atlantic-water-flow/ 思路是从海洋开始逆流 如果可以逆流到 就标记为1 然后检查两个海洋都可以逆流到的区域 DFS public List<List<Integer>> pacificAtlantic(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) { 阅读详情

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:

The order of returned grid coordinates does not matter.
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).

给出一个矩阵,每个元素代表一个大陆的高度,水只能向上下左右四个方向流,且只能从高向低流或者流向相同的高度。
矩阵的第一行和第一列代表太平洋( p), 最后一列和最后一行代表大西洋(a),要求输出能同时流向p和a的陆地的index(row, col)

思路:
可以反向思维,分别从太平洋和大西洋的两条边出发,反向找到能到达的点,然后取它们的交集即可。
分别建立太平洋 ( p) 和大西洋(a) 的boolean矩阵作为visited矩阵,寻找点的过程用DFS,从第一行和第一列出发时更新p矩阵,最后一行和最后一列时更新a矩阵。当已经访问过时直接return,而且还有一个条件,就是水只能从高往低流,因此DFS下一个邻居的height < 当前height时直接return。
最后遍历所有点,取p和a同时为true的行列装进结果的list中。

class Solution {
    int rows = 0;
    int cols = 0;
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(matrix == null || matrix.length == 0) {
            return result;
        }
        
        rows = matrix.length;
        cols = matrix[0].length;
        
        boolean[][] p = new boolean[rows][cols];
        boolean[][] a = new boolean[rows][cols];
        int[][] v = new int[rows][cols];
        
        //first and last col
        for(int r = 0; r < rows; r++) {
            dfs(matrix, r, 0, 0, p);
            dfs(matrix, r, cols - 1, 0, a);
        }
        
        //first and last row
        for(int c = 0; c < cols; c++) {
            dfs(matrix, 0, c, 0, p);
            dfs(matrix, rows - 1, c, 0, a);
        }
        
        for(int r = 0; r < rows; r++) {
            for(int c = 0; c < cols; c++) {
                if(p[r][c] && a[r][c]) {
                    List<Integer> tmp = new ArrayList<>();
                    tmp.add(r);
                    tmp.add(c);
                    result.add(tmp);
                }
            }
        }
        
        return result;
    }
    
    public void dfs(int[][] matrix, int r, int c, int height, boolean[][] v){
        if(r < 0 || c < 0 || r >= rows || c >= cols) {
            return;
        }
        
        if(matrix[r][c] < height || v[r][c]) {
            return;
        }
        
        v[r][c] = true;
        dfs(matrix, r, c + 1, matrix[r][c], v);
        dfs(matrix, r, c - 1, matrix[r][c], v);
        dfs(matrix, r + 1, c, matrix[r][c], v);
        dfs(matrix, r - 1, c, matrix[r][c], v);
    }
}
ModbusRTU串口读写报文解析 解析MODBUS报文的格式,讨论异常状态、读多个寄存器与写多个寄存器 阅读详情

相关推荐

python数据处理与分析案例,python如何进行数据处理

大家好,本文将围绕基于python的数据处理案例展开说明,python数据处理与分析案例是一个很多人都想弄明白的事情,想搞清楚python如何进行数据处理需要先了解以下几个事情。大家好,小编来为大家解答以下问题,python数据处理案例步骤及其代码,python数据处理与分析教程,今天让我们一起来看看吧!哈喽~今天学习记录的是数据分析实训小案例。就用这个案例来好好巩固一下 python 数据分析三剑客。

a17348的博客 1242

CF c题

C. Make Equal With Mod time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an array ofnnnon-negative integersa1,a2,…,ana1,a2,…,an. You can make the following operation: cho...

m0_61949623的博客 533

谷粒商城 - 个人笔记(集群部署篇一)

电商后台管理系统+电商系统,掌握微服务的全套方案。该系统有商品服务、仓储服务、订单服务、购物车服务、检索服务、认证中心服务、网关服务、用户服务、秒杀服务、第三方服务。使用SpringBoot+SpringCloud并配套SpringCloud Alibaba系列,引入全套微服务治理方案:Nacos注册中心/配置中心、Feign远程调用、Gateway网关、Redisson缓存、基于ElasticSearch7全文检索、异步编排与线程池、RabbitMQ柔性事务方案、Redisson分布式锁、分布式信号量等。

知行 4020

417. Pacific Atlantic Water Flow

417. Pacific Atlantic Water Flow方法0: wrong方法2: dfs方法2: dfs 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...

wilzxu的博客 415

PCL点云DBSCAN聚类算法:基于密度的点云聚类

核心点是在给定半径内拥有足够多邻居点的点,边界点是在给定半径内没有足够多邻居点但是位于核心点的邻域内的点,噪声点是既不是核心点也不是边界点的点。本文将介绍PCL中的DBSCAN(基于密度的空间聚类应用噪声)算法,用于点云数据的聚类。上述示例代码首先从PCD文件中读取点云数据,并设置了DBSCAN聚类的参数,包括邻域搜索半径(epsilon)和邻域内最小点数(min_points)。需要注意的是,示例代码中的点云数据格式为pcl::PointXYZ,如果使用其他类型的点云数据,需要相应地修改代码。

LpmShell的博客 1247

实现AI Agent的多Agent协作能力

在当今复杂的计算环境和应用场景中,单个AI Agent的能力往往受到限制。实现AI Agent的多Agent协作能力的目的在于通过多个Agent之间的协同工作,充分发挥各自的优势,提高整体系统的性能和效率,以解决更为复杂的问题。本文章的范围涵盖了多Agent协作的核心概念、算法原理、数学模型、项目实战以及实际应用场景等方面,旨在为读者提供全面且深入的技术指导。本文将按照以下结构进行阐述:首先介绍多Agent协作的背景知识,包括目的范围、预期读者和文档结构等;

AI Python 编程之道的博客 1486

LeetCode 417. Pacific Atlantic Water Flow _太平洋大西洋水流问题

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

桀客 546

你分得清ChatGPT、Gemini、Grok、豆包、千问、DeepSeek、OpenClaw、CloudCode、vibeCodeing、sKill、MCP,飞书、微信这些到底是什么吗?

AI技术生态呈现层级化发展,从基础模型到应用平台形成完整技术栈。核心分为五层:模型层(GPT、Gemini等基础模型)→AI助手层(ChatGPT等应用产品)→工具层(ClaudeCode等开发工具)→协议层(MCP等接口标准)→应用层(微信、飞书等平台)。未来趋势将形成"模型公司-开发框架-应用生态"三层结构,AI正逐步演变为新型操作系统,通过标准化协议连接各类外部系统,实现能力调用与生态整合。技术发展呈现出从底层基础设施到上层应用场景的垂直整合特征。

hy行者勇哥的博客 408

深度/广度优先搜索LeetCode 417 Pacific Atlantic Water Flow

LeetCode 417 Pacific Atlantic Water Flow Problem Description: 题目给出一个矩阵,矩阵的左侧和上侧为太平洋区域,矩阵的右侧和下侧为大西洋区域,矩阵中每个元素值代表水的高度,水只能向不比它高的地方流动。题目要求矩阵中哪些位置的水既可以流向太平洋也能流向大西洋。 具体的题目信息: https://leetcode.com/prob...

萤火虫啊飞呀飞 374

C# WPF上位机开发(利用tcp/ip网络访问plc)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 c# wpf如果是用来开发非标上位机的,那么和plc的通信肯定是少不了的。而且,大部分plc都支持modbus协议,所以这个时候如果有一个库可以帮助我们和plc设备进行modbus通信,那就非常方便的。目前NuGet下关于modbus的函数库还是有一些的,比如EasyModbus。

嵌入式-老费,一个分享专业嵌入式知识的blog 2532

【R-terra包|第一节】- 栅格相关性、散点图 、波段的计算与异常值处理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 —```mermaid flowchat st=>start: 开始 e=>end: 结束 op=>operation: 我的操作 cond=>condition: 确认? st->op->cond cond(yes)->e cond(no)->op 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能.

地理空间科学的博客 5617

【LeetCode】417. Pacific Atlantic Water Flow 太平洋大西洋水流问题

有一个 m × n 的矩形岛屿,与 太平洋 和 大西洋 相邻。“太平洋”处于大陆的左边界和上边界,而 “大西洋” 处于大陆的右边界和下边界。 这个岛被分割成一个由若干方形单元格组成的网格。给定一个 m x n 的整数矩阵heights,heights[r][c]表示坐标 (r, c) 上单元格 高于海平面的高度 。 岛上雨水较多,如果相邻单元格的高度 小于或等于 当前单元格的高度,雨水可以直接向北、南、东、西流向相邻单元格。水可以从海洋附近的任何单元格流入海洋。 返回网格坐标 result

负雪明烛 4246

E2B Agent Runtime Sandbox 沙箱技术架构研究

本实现指南基于E2B的技术架构,提供了完整的基于Firecracker和Docker的Agent Runtime Sandbox后端服务实现方案。架构设计:参考E2B的分层架构,实现API层、管理层和虚拟化层Firecracker集成:深度集成Firecracker提供硬件级安全隔离完整功能:支持文件读写、shell执行、浏览器访问等核心功能生产就绪:提供Docker部署、监控、测试和安全配置可扩展性:设计支持水平扩展和高并发场景。

AI天才研究院 772

LeetCode //C - 417. Pacific Atlantic Water Flow

【代码】LeetCode //C - 417. Pacific Atlantic Water Flow。

Made in Code 1088

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Leetcode-417. Pacific Atlantic Water Flow

前言:正好碰见Leetcode有一次在线笔试,测试一下,还是挺开心的,全做出来了Rank:98/869。如有错误欢迎指正。博主首发CSDN,mcf171专栏。 博客链接:mcf171的博客 —————————————————————————————— Given an m x n matrix of non-negative integers representing t

mcf171的专栏 1759

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】

LeetCode 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Medium】【Python】【DFS】 Problem LeetCode Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, th...

Wonz 677

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LeetCode 417. 太平洋大西洋水流问题(BFS/DFS)

文章目录1. 题目2. 解题2.1 BFS 广度优先搜索2.2 DFS 深度优先搜索 1. 题目 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。 “太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。 规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。 请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。 提示: 输出坐标的顺序不重要 m 和 n 都小于150 示例: 给定下面的 5x5 矩阵: 太平

Michael是个半路程序员 1820

leetCode No.417 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” touc

Andy_Shan的博客 1470

leetcode 417. Pacific Atlantic Water Flow 太平洋大西洋水流问题

逆向思维,假设海水涨潮的时候,太平洋和大西洋的水能漫到陆地的最高点是多少,分别记录下来。假设水在到达最高点后不在继续漫了。这时分别记录下太平洋和大西洋的水能到达的位置。再取他们公共的部分就是答案了。

ln_ydc的专栏 252

LeetCode-417. Pacific Atlantic Water Flow [C++][Java]

There is anm x nrectangular island that borders both thePacific OceanandAtlantic Ocean. ThePacific Oceantouches the island's left and top edges, and theAtlantic Oceantouches the island's right and bottom edges.

贫道绝缘子的博客 618

[LeetCode 417] Pacific Atlantic Water Flow

Given anm x nmatrix 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" touch...

ExcitedZhang的博客 265

【leetcode】【medium】417. Pacific Atlantic Water Flow​​​​​​​

417. Pacific Atlantic Water Flow Given anm x nmatrix 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 mat...

lemonade13的博客 166

leetcode 417. Pacific Atlantic Water Flow | 417. 太平洋大西洋水流问题(DFS,经典“感染”思路)

题目 https://leetcode.com/problems/pacific-atlantic-water-flow/ 题解 一开始的错误思路:(区分“主动”与“被动”的过程) 正确答案参考视频:https://leetcode-cn.com/problems/pacific-atlantic-water-flow/solution/java-si-lu-qing-xi-dai-ma-jian-ji-by-ven-4cds/ 看了视频的思路之后,发现其实这是一个经典的“感染”问题…代码迅速搞定。

寒泉 4万+

[leetcode] 417. Pacific Atlantic Water Flow

Description 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 ...

Learning from the mistakes 189

LeetCode 417. Pacific Atlantic Water Flow

Given anm x nmatrix 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" touch...

qq_39638957的博客 226

[Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow

原文地址:https://www.cnblogs.com/strengthen/p/10332885.html Given anm x nmatrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left ...

weixin_30873847的博客 130
上一篇: leetcode 394. Decode String(解码字符串)
下一篇: leetcode 104.Maximum Depth of Binary Tree(二叉树的最大深度)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址