LeetCode 200. Number of Islands

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

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 the grid are all surrounded by water.
Example 1:

11110
11010
11000
00000

Answer: 1
Example 2:

11000
11000
00100
00011

Answer: 3

题意:给你一个二维网图,‘1’代表陆地‘0’代表水。一个被水环绕的岛由水平或垂直邻接的陆地组成。假设网图的四个边被水环绕。
样例可以一眼就看出有多少个岛。

解题思路:我们可以访问每一个land,然后访问其周围的land,访问过后将land标记为water防止再次访问。

class Solution {
public:
    /*
        访问所有的点:
        1.遇到'0'(water)我们就返回
        2.遇到'1'(land),我们就把这个点设置成'0'(water),然后继续搜索其上下左右的点

        Custom testcase:
        [["1","1","1"],["0","1","0"],["1","1","1"]] 让我Wrong Answer了一次(只访问了下边和右边)
    */
    int numIslands(vector<vector<char>>& grid) {
        if(grid.empty()) return 0;
        int rows = grid.size(), cols = grid[0].size();

        int lands = 0;  // land总数
        // 遍历每一个点,遇到新的land时,++lands
        for(int r = 0; r < rows; ++r){
            for(int c = 0; c < cols; ++c){
                if(grid[r][c] == '1'){
                    searchLand(r, c, grid);
                    ++lands;
                }
            }
        }
        return lands;
    }

private:
    void searchLand(int row, int col, vector<vector<char>>& grid){
        if(row >= grid.size() || col >= grid[0].size() || row < 0 || col < 0) return;   // 边界
        if(grid[row][col] == '0') return;   // 遇到了water

        grid[row][col] = '0';  // 将该节点设置为'0'
        searchLand(row - 1, col, grid);    // 访问上边的点
        searchLand(row + 1, col, grid);    // 访问下边的点
        searchLand(row, col - 1, grid);    // 访问左边的点
        searchLand(row, col + 1, grid);    // 访问右边的点
    }
};

Time Complexity: 访问了所有的节点,即O(n*m)
Space Complexity: 取决于递归栈的深度

这里写图片描述

Cheat Engine实战:多级指针逆向破解游戏内存的5个关键步骤(附C++模拟代码) 本文详细解析了使用Cheat Engine进行多级指针逆向破解游戏内存的完整流程。通过5个关键步骤,从定位动态地址到逐层追踪指针链,最终构建稳定指针路径。文章结合C++模拟代码,深入剖析了指针链的底层汇编原理,并提供了高级排查技巧,帮助读者掌握逆向工程中的核心内存追踪技术。 阅读详情

相关推荐

【LeetCode】200. Number of Islands 岛屿数量

【LeetCode】200. Number of Islands 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/number-of-isla...

负雪明烛 1万+

ESP32-S3开发板烧录失败?Bootloader模式进入全攻略

本文详细解析了ESP32-S3开发板烧录失败的常见原因及解决方案,重点介绍了如何正确进入Bootloader模式。通过按键组合、硬件设计差异分析、自动下载模式失效排查等实用技巧,帮助开发者高效解决烧录问题,提升开发效率。

weixin_28716769的博客 581

LeetCode 200. Number of Islands 题解

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

cannonjinx的博客 1005

Spacedesk保姆级教程:用旧平板给Windows电脑当副屏,从安装到避坑一步到位

本文提供Spacedesk保姆级教程,教你如何将旧平板变成Windows电脑的副屏,从软件安装到网络配置、显示优化及声音交互,一步步避开常见陷阱。通过WiFi实现扩展屏功能,支持4K分辨率和60Hz刷新率,让闲置设备焕发新生。

weixin_42524945的博客 1万+

一种高超声速飞行器弹道的仿真方法

设计并实现了一个高超声速飞行器弹道仿真算法,并应用到基于OSGEarth的态势中。

eyepeak的博客 6541

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

NVIDIA Jetson AGX Xavier设置风扇转速、工作模式及性能监控

Xavier的风扇在开机之后是默认关闭的,每次重启都需要手动打开风扇,后续可能考虑修改bashrc文件进行自动开启 这个是显示当前风扇的转速,默认应该是0,表示关闭,最大是250 cat /sys/devices/pwm-fan/target_pwm 可以通过修改该文件的值来修改风扇转速 vim /sys/devices/pwm-fan/target_pwm 我一般使用的是100这个值 作者:ryan_ren 链接:https://www.jianshu.com/p/d3c2041f7f7d 来源:简书 著

weixin_43877080的博客 1万+

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

PMBus协议解析:基于I2C的电源管理实战与TMS320F28003x驱动开发

在嵌入式系统与服务器电源设计中,数字电源管理总线是实现高效、可靠电源监控与控制的关键技术。PMBus协议基于广泛应用的I2C总线物理层,通过定义标准化的命令集、数据格式与时序规范,为多路电源的电压电流监控、动态调整及故障处理提供了高效解决方案。其技术价值在于通过硬件自动处理协议解析、时钟拉伸与中断,显著降低软件复杂度,使开发者能聚焦于电源环路设计等核心逻辑。该协议广泛应用于服务器、通信基站及高性能计算板卡等场景,通过引入数据包错误检查、专用告警线等机制,确保了电源管理的可靠性。本文以TI TMS320F28

weixin_29189377的博客 374

华为od 面试八股文_操作系统_01_含答案

华为od 面试八股文_操作系统_01_含答案

MISAYAONE的博客 3269

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

37、OpenMP:并行编程的强大工具

本文详细介绍了 OpenMP 的核心功能和使用方法,包括执行环境例程、线程同步与协调机制,如关键区域、屏障、原子操作、归约子句、刷新构造和锁机制。此外,还提供了多个实践练习,涵盖矩阵乘法的并行实现、任务池与流水线模式的应用、读写锁的扩展与移植,以及客户端-服务器模式的实现与优化。通过这些内容,读者可以全面掌握 OpenMP 在并行编程中的实际应用与性能优化技巧。

www00的博客 101

【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

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【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

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

leetcode 200 : Number of Islands

leetcode 200 : Number of Islands java c++ python

西施豆腐渣 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Leetcode200. Number of Islands

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

magic_jiayu的博客 188

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

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

LeetCode200—Number of Islands

LeetCode200—Number of Islands

上善若水 1074

深度/广度优先搜索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 152. Maximum Product Subarray
下一篇: LeetCode 221. Maximal Square
Spade_
博客等级 码龄10年 918粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值