Leetcode-Number of Islands

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

今天看到Leetcode又有新题,故着手去做。题目比较简单,就是dfs或bfs的应用。

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.

题意:给定一个2维的网格地图,地图上为字符“1”或“0”,其中“1”为陆地,“0”为水域,默认地图周围都是由水构成。相连的陆地算作一个岛屿,求出地图中的岛屿总数。比如:

11110

11010

11000

00000

为1个岛屿。

11000

11000

00100

00011

为3个岛屿。

分析:只需要对每一个陆地区域做一次dfs,每次dfs中将已经遍历的陆地网格“1”变为水域网格“0”(防止再次遍历导致重复)。对每次dfs计数,总共dfs的次数即为岛屿总数。应注意,grid为空的特殊情况应该排除。

AC代码:

class Solution {
public:
    int numIslands(vector<vector<char>> &grid) {
        int row = grid.size();
        if (!row) return 0;
        int col = grid[0].size();
        int count = 0;

        for (unsigned i = 0; i != row; ++i) {
        	for (unsigned j = 0; j != col; ++j) {
        		if (grid[i][j] == '1') {
        			dfs(grid, i, j); ++count;
        		}
        	}
        }

        return count;
    }
    void dfs(vector<vector<char>> &grid, unsigned i, unsigned j) {
    	int dx[4] = {1, 0, -1, 0};
    	int dy[4] = {0, 1, 0, -1};
    	int row = grid.size(), col = grid[0].size();
    	grid[i][j] = '0';

    	for (int k = 0; k != 4; ++k) {
    		int new_i = i + dx[k], new_j = j + dy[k];
    		if (new_i >=0 && new_i < row && new_j >= 0 && new_j < col && grid[new_i][new_j] == '1')
    			dfs(grid, new_i, new_j);
    	}
    }
};
如代码或分析有误,请批评指正,谢谢!




手把手教你用STM32F103C8T6驱动CLRC663国产NFC模块(兼容A/B/F/15693卡) 本文详细介绍了如何使用STM32F103C8T6驱动国产CLRC663 NFC模块,支持ISO14443A/B、Felica和ISO15693协议。从硬件设计、协议适配到状态监控,提供完整的工程实现方案,适用于智能门锁、支付终端等应用场景。 阅读详情

相关推荐

三维荧光光谱(EEM)散射干扰实战:从识别到精准切除的完整流程

本文详细解析三维荧光光谱(EEM)中散射干扰的识别与精准切除流程,涵盖瑞利散射和拉曼散射的实战处理技巧。通过drEEM工具箱的应用示例和高级场景解决方案,帮助研究者有效消除散射干扰,提升数据准确性,特别适用于环境样品和生物活体样本分析。

weixin_42554042的博客 334

【Leetcode】Number of Islands

题目链接:https://leetcode.com/problems/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 co

业精于勤,荒于嬉 1000

STGCN实战:从零构建PyTorch时空图卷积网络预测城市交通流

本文详细介绍了如何使用PyTorch从零构建时空图卷积网络(STGCN)进行城市交通流预测。通过将交通网络建模为图结构,并融合时间卷积与空间图卷积,STGCN能有效捕捉交通流的时空依赖性。文章提供了从数据预处理、核心模块(TimeBlock与STGCNBlock)搭建、模型训练到实战技巧的完整指南,帮助开发者快速掌握这一前沿的交通速度预测技术。

wasm7browser的博客 256

[leetcode] 305. Number of Islands II 解题报告

题目链接: https://leetcode.com/problems/number-of-islands-ii/ A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at posi

小榕流光的专栏 5064

别再手动拆解字节了!用博图这个功能指令,5分钟搞定ARRAY转BOOL数组

本文详细介绍了西门子博图中`ARRAY_TO_BOOL`功能指令的高效应用,帮助工程师快速实现ARRAY到BOOL数组的转换。通过实战案例和参数配置指南,展示了如何5分钟内完成传统方法需半小时的任务,大幅提升PLC编程效率,特别适用于通信报文解析和设备状态监控。

weixin_42653691的博客 268

从零到一:基于Rocky Linux 9的Kubernetes高可用集群部署实战

本文提供了一份基于Rocky Linux 9操作系统,从零开始部署Kubernetes高可用集群的完整实战指南。详细讲解了包括系统初始化、容器运行时配置、使用kubeadm工具进行集群初始化、节点加入以及Calico网络插件安装在内的核心步骤,并针对国内网络环境提供了镜像加速等优化方案,旨在帮助读者构建一个稳定可靠的生产级K8s环境。

weixin_29197835的博客 151

Leetcode 305. Number of Islands II (python)

Leetcode 305. Number of Islands II题目:解法:hashmap 题目: 解法:hashmap 这道题是number of islands的follow up,关于number of islands见另外一篇博客。 这道题目的难点在于,

qq_37821701的博客 1211

Camera tuning flow相机调试流程

相机调试流程分为三大模块:ProcessRaw处理原始数据(黑电平校正、阴影校正),3A实现自动控制(曝光/对焦/白平衡),ISP进行图像精加工(伽马校正、锐化降噪、色彩处理)。三个阶段环环相扣,前阶段为后阶段提供基础,最终输出画质优良且风格化的图像。

weixin_64706696的博客 1233

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 544

Linux驱动:基于imx6ull分析PWM驱动及backlight屏幕背光驱动

文章目录1、PWM驱动1.1 驱动分析1.2 使用方法2、backlight驱动2.1 驱动分析2.2 调试方法 1、PWM驱动 1.1 驱动分析 先看设备树文件imx6ull.dtsi的PWM节点,以PWM1为例(绑定说明:Documentation/devicetree/bindings/pwm/imx-pwm.txt): pwm1: pwm@02080000 { compatible = "fsl,imx6ul-pwm", "fsl,imx27-pwm"; reg = <0x02

weixin_44498318的博客 1963

【Bug已解决】Claude Desktop Cowork 报错 Workspace unavailable 解决方案

方案适用场景推荐指数确认虚拟机镜像下载完整性排查的第一步,最基础的前提检查⭐⭐⭐⭐⭐确认虚拟机平台功能已启用Windows 系统功能层面的必要条件⭐⭐⭐⭐⭐重启 CoworkVMService快速尝试恢复服务正常状态⭐⭐⭐⭐完全重启客户端排除临时性状态错乱⭐⭐⭐⭐降级使用文件工具临时替代方案,不阻塞当前工作⭐⭐⭐报错的本质是Cowork 功能依赖的虚拟机管理服务,在 Windows 应用包隔离机制下未能正确定位虚拟机文件的实际存放路径。

博客 6280

Leetcode200. Number of Islands

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

magic_jiayu的博客 190

WCK Operation (WCK 操作)

LPDDR6 SDRAM采用双时钟系统(WCK和CK),其中WCK频率是CK的两倍。为确保数据正确传输,需通过WCK2CK同步操作使两种时钟对齐。LPDDR6支持两种同步模式:Auto-sync off模式和WCK Always on模式,可通过MR22寄存器选择。同步操作由特定命令(Read/Write/MRR等)触发,需在满足特定时序参数(如tWCKENL、tWCKPRE等)后完成。同步前必须完成WCK2CK训练,且不同操作(读取/写入/CAS)有各自的时序要求。该机制确保数据在正确时钟相位下传输,是L

李家小院 666

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

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

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode 200. Number of Islands 题解

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

cannonjinx的博客 1007

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

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

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

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

个人博客 998

[LeetCode]200. Number of Islands

https://leetcode.com/problems/number-of-islands/ 判断有多少相连的岛屿。 解法一:dfs,每次遇到1就把它置为0 public class Solution { int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public int numIslands(c

gqk289的专栏 238

【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的博客 778

[LeetCode][JavaScript]Number of Islands

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

weixin_30813225的博客 101

Leetcode Number of Islands

题目地址:https://leetcode.com/problems/number-of-islands/ 题目分析: 需要找出水平或者垂直相连的‘1’的个数,第一思路是递归遍历直到上下左右走没有‘1’返回,同时需要注意遍历到一个‘1’将其修改为‘0’表示已经访问过了;然后每遍历完一次即表示一个island,设置一个计数器记录遍历次数即可。 题目解答: public class Sol...

weixin_34114823的博客 108

[LeetCode]Number of Islands

原题链接:https://leetcode.com/problems/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 connec...

weixin_30394251的博客 92

leetcode Number of Islands

题目连接 https://leetcode.com/problems/number-of-islands/ 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 surrounded by w...

weixin_30466421的博客 75

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map ofmrows andncolumns is initially filled with water. We may perform anaddLandoperation which turns the water at position (row, col) into a land. Given a list ...

weixin_33928137的博客 438

LeetCode题解–200. Number of Islands

链接LeetCode题目: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 form

sinat_14867719的博客 483
上一篇: python实现清华大学联网助手(三)——linux下带参数的自定义命令/import机制
下一篇: Leetcode201-Bitwise AND of Numbers Range
coderhuhy
博客等级 码龄12年 24粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值