【leetcode】 200. Number of Islands

leetcode解题之200. Number of Islands Java版(岛屿的数量) leetcode解题之200. Number of Islands Java版(岛屿的数量) ,深度优先遍历 阅读详情

简单的dfs递归,我们把每个访问到的‘1’,都对其进行dfs,使得与他相邻的‘1’都置为‘0’

/**
 * @author          johnsondu
 * @problem         Number of Islands
 * @url             https://leetcode.com/problems/number-of-islands/
 * @timeComlexity   O(n^2)
 * @spaceComplexity O(1)
 * @strategy        See code.
 */
 
 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
class Solution {
public:
    void dfs(int x, int y, int row, int col, vector<vector<char>>& grid)
    {
        for(int i = 0; i < 4; i ++) {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(tx < row && tx >= 0 && ty < col && ty >= 0) {
                if(grid[tx][ty] == '1') {
                    grid[tx][ty] = '0';
                    dfs(tx, ty, row, col, grid);
                }
            }
        }
    }

    int numIslands(vector<vector<char>>& grid) {
        int row = grid.size();
        if(row == 0) return 0;
        int col = grid[0].size();
        if(col == 0) return 0;
        
        int cnt = 0;
        for(int i = 0; i < row; i ++)
            for(int j = 0; j < col; j ++) {
                if(grid[i][j] == '1') {
                    cnt ++;
                    dfs(i, j, row, col, grid);
                }
            }
        return cnt;
    }
};


华为云HCS解决方案笔记HUAWEI CLOUD Stack【面试篇】 华为云解决方案介绍、华为云服务介绍,华为云网络平面规划等 阅读详情

相关推荐

ALTERA MAX10 FPGA 串口远程升级实战:从原理到烧录的避坑指南

本文详细解析了ALTERA MAX10 FPGA如何通过串口实现远程升级。文章从工程师实战经验出发,深入剖析了MAX10片内Flash的双镜像备份原理、CONFIG_SEL引脚启动机制,并重点介绍了使用On-Chip Flash IP核进行擦写操作的核心流程。同时,提供了从Quartus工程配置、RPD文件生成、串口协议设计到驱动代码编写的完整避坑指南,旨在帮助开发者稳定、高效地完成远程升级功能部署。

weixin_29197699的博客 273

LeetCode 200. Number of Islands 题解

本题是图论中连通分量计数的经典问题。遍历网格,找到未访问的陆地使用 DFS 或 BFS 标记整个岛屿统计岛屿数量。

cannonjinx的博客 1005

智能体评测技术与实践:从评估维度到DeepEval实战指南

智能体评测技术与实践:从评估维度到DeepEval实战指南 本文系统探讨了AI智能体的评测体系与实践方法。首先从四个关键维度构建评估框架:基础能力(意图理解、知识完整性)、复杂任务(多轮对话、动态工作流)、可靠性(幻觉抑制、安全合规)以及工具效率(参数填充、多工具协同)。其次分析了主流评测工具,包括xBench的动态双轨评估、DeepEval的全链路监控等解决方案。最后详细介绍了DeepEval的实战应用,涵盖环境配置、测试用例构建、指标配置及CI/CD集成,展示了如何通过代码化评估实现智能体质量的持续提升

Linda的自动化测试之旅 3413

Leetcode 200. Number of Islands

Leetcode 200. Number of Islands Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontal...

qwfand的博客 168

从ORCAP-1589警告看Cadence ORCAD的DRC设置:如何避免‘网络别名’引发的潜在短路风险

本文深入解析Cadence ORCAD中ORCAP-1589警告的深层含义,探讨网络别名可能引发的短路风险及DRC设置优化方案。通过分析网络别名的设计意图、DRC规则配置及最佳实践,帮助工程师有效规避设计风险,提升电路设计的可靠性和规范性。

weixin_34194829的博客 297

PCIe中ACS知识小记

虚拟机内部的访问数据显而易见是带的GPA地址。而目前几乎所有的PCIe 设备都带ACS control,所以它们可以单独assign给不同的VM,但是随之而来的就是即使两个GPU同时assign给同一个VM,由于ACS的存在,他们之间也不会有真正的物理层面的P2P访问。每一个TLP都自带一个address(寻址类TLP),同时每一个endpoint,PCIe switch都自带一段BIOS配置的地址空间范围,一旦这个TLP的address落入某个设备的地址范围,那么这个TLP就属于这个设备并消费。

Summer的博客 5077

LeetCode 200. Number of Islands--c++ dfs解法

LeetCode 200. Number of Islands LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目Java和Python的解法都有。 Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island i...

个人博客 995

Altium Designer(AD24/25)导出DXF或DWG结构文件的方法

DXF(Drawing Exchange Format)和DWG(Drawing)是两种常见的CAD(计算机辅助设计)文件格式,常用于结构和硬件之间的沟通。本文介绍Altium Designer(AD24/25)导出DXF或DWG结构文件的方法。

fydar的博客 2313

【LeetCode with JavaScript】 200. Number of Islands

原题网址:https://leetcode.com/problems/number-of-islands 难度:Medium 题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by...

zwkkkk1的博客 775

ofd转pdf报错:org.ofdrw.reader.ZipUtil.unZipFileByApacheCommonCompress【已解决】

ofd转pdf报错:java.lang.NoClassDefFoundError:org/apache/commons/io/function/IOIterator at org.ofdrw.reader.ZipUtil.unZipFileByApacheCommonCompress,java.lang.NoSuchMethodError:org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.getNextEntry()

黄团团的个人博客 1283

新手友好!C#上位机调用YOLO实现设备异常检测(详细步骤,附踩坑指南)

C#上位机调用YOLO实现设备异常检测,核心是把“工控交互”和“AI检测”的优势结合起来,新手不用追求一步到位,先跑通基础流程,再逐步优化性能和功能。我当初也是从踩坑中过来的,只要把核心链路和细节摸透,其实门槛并没有想象中高。如果你们在实操中遇到了具体问题(比如相机取流卡顿、模型推理报错),可以在评论区留言,后续我也可以针对性拆解具体模块的完整代码。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 663

LeetCode刷题:200. Number of Islands

LeetCode刷题:200. Number of Islands 原题链接:https://leetcode.com/problems/number-of-islands/description/ Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is sur...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 702

Allegro172版本如何在联机模式下使用skill文件操作指导

注意:如果下次重新开启Symphony模式的时候可能导致symphony_skill中的文件内容被清空,所以最好将自己常用的skill文件都写到symphony_skill.txt中去,然后做好备份,需要的时候,在开启软件后把文件放到env目录中去。在用Allegeo172版本进行PCB设计的时候,有时会用到Symphony模式进行多人实时在线协同设计,如下图。这个模式设计起来可以提升设计速度,但是也有一个缺陷,就是不能使用skill文件。如下图,当调用skill的时候就会出现如下报错。

weixin_54787054的博客 1586

【LeetCode】200. Number of Islands 岛屿数量(Medium)(JAVA)

【LeetCode】200. Number of Islands 岛屿数量(Medium)(JAVA) 题目地址: https://leetcode.com/problems/number-of-islands/ 题目描述: Given an m x n 2d grid map of '1’s (land) and '0’s (water), return the number of islands. An island is surrounded by water and is formed by con

qq_16927853的博客 193

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode 200 : Number of Islands

leetcode 200 : Number of Islands java c++ python

西施豆腐渣 1万+

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Leetcode200. Number of Islands

一个二维数组,把 1 看做陆地,把 0 看做大海,陆地相连组成一个岛屿。把数组以外的区域也看做是大海,问总共有多少个岛屿。

magic_jiayu的博客 188

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Leetcode 200 Number of Islands 并查集

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assu

无名山丘,崛起成峰 2703

LeetCode200. Number of Islands 基础dfs

题目:https://leetcode.com/problems/number-of-islands/description/ dfs寻找联通区域个数,使用递归会超时,要用栈模拟递归,代码如下: class Solution { public: void dfs(vector> grid,vector> &visited ,int i,int j){

浅时光_XinLee 543

200. Number of Islands【力扣】

题意理解 在一张n×m图上,统计1聚在一起的团的数量 问题分析 递归 找到一个小岛,邻接小岛都不需要统计,直接把它们设成海洋即可。先将本身小岛设置成海洋,然后递归遍历上下左右的区域,如果是小岛就设置成海洋,如果是海洋就遍历结束。 其他 这是个好题。学习 链接 int numIslands(vector<vector<char>>& grid...

xiexie1357的专栏 422

LeetCode 200. Number of Islands(dfs)

题目来源:https://leetcode.com/problems/number-of-islands/ 问题描述 200.Number of Islands Medium Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is surrounded b...

da_kao_la的博客 207

LeetCode200—Number of Islands

LeetCode200—Number of Islands

上善若水 1074

LeetCode Top 100 Liked Questions 200. Number of Islands (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 200. Number of Islands (Java版; Medium) 题目描述 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounde...

littlehaes的博客 342

LeetCode 200. Number of Islands

LeetCode 200. Number of Islands (Medium) 主要知识点:二维DFS、二维BFS、Union Find;优先级:5;

温迪今天刷题了吗 327

深度/广度优先搜索LeetCode 200 Number of Islands

LeetCode 200 Number of Islands Problem Description: 题目给出一个0-1矩阵,0表示水面,1表示陆地,要求求出该矩阵共有多少块陆地。其中,若两块陆地(两个1)相邻(一个在另一个的左方、右方、上方和下方),则可merge为一块陆地。 具体的题目信息: https://leetcode.com/problems/number-of-isla...

萤火虫啊飞呀飞 261

LeetCode - 解题笔记 - 200 - Number of Islands

【备注】查看官方题解,还给出了并查集的思路,也就是通过并查集标记所属团体,并通过不断地find和union将关联集合数量减至最低(但是效率觉得反而变差了)也就根据给定map的四邻域判断有多少个联通分区域。使用BFS在没有搜索过的“1”标记点上开始BFS,并不断标记找过的位置。调用BFS的次数即为岛屿总数。Solution 1的Python实现。非常经典的BFS模版题。

支锦铭的博客 368

Leetcode200 Number of Islands解题方法

Leetcode200 Number of Islands 题目描述 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of th

初七的博客 180
上一篇: 【leetcode】 225. Implement Stack using Queues
下一篇: 【hihocoder】1014. Trie树
小do在努力
博客等级 码龄16年 58粉丝 161原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值