【leetcode】200. (Medium)Number of Islands

wifi/BT芯片RTL8723DS驱动移植和测试之一wifi篇 本文主要介绍如何在stm32mp157中移植并测试芯片RTL8723DS的wifi功能。 阅读详情
解题思路:

DFS


提交代码:

class Solution {
    public int numIslands(char[][] grid) {
    	if(grid==null||grid.length==0)	return 0;
    	int[][] tmp=new int[grid.length][grid[0].length];
    	
    	int cnt=0;
    	for(int i=0;i<grid.length;i++) {
    		for(int j=0;j<grid[0].length;j++) {
    			if(grid[i][j]=='1')
    				cnt+=check(i,j,grid,tmp);
    		}
    	}
    	
    	return cnt;
    }
    
    public int check(int row,int col,char[][] grid,int[][] tmp) {
    	if(tmp[row][col]==1)	return 0;
    	
    	tmp[row][col]=1;
    	findRemain(row,col,grid,tmp);
    	return 1;
    }
    
    public void findRemain(int row,int col,char[][] grid,int[][] tmp) {
    	if(row!=0) {
    		if(grid[row-1][col]=='1'&&tmp[row-1][col]==0) {
    			tmp[row-1][col]=1;
    			findRemain(row-1,col,grid,tmp);
    		}
    	}
    	
    	if(col!=0) {
    		if(grid[row][col-1]=='1'&&tmp[row][col-1]==0) {
    			tmp[row][col-1]=1;
    			findRemain(row,col-1,grid,tmp);
    		}
    	}
    	
    	if(row!=grid.length-1) {
    		if(grid[row+1][col]=='1'&&tmp[row+1][col]==0) {
    			tmp[row+1][col]=1;
    			findRemain(row+1,col,grid,tmp);
    		}
    	}
    	
    	if(col!=grid[0].length-1) {
    		if(grid[row][col+1]=='1'&&tmp[row][col+1]==0) {
    			tmp[row][col+1]=1;
    			findRemain(row,col+1,grid,tmp);
    		}
    	}
    }
}

check函数用于检查当前陆地部分是否已经属于一个已知的岛屿,如果当前陆地不属于一个已知的岛屿,则将当前陆地标记为一个新的岛屿,并用findRemain函数找到和当前陆地连接起来的部分。


运行结果:
在这里插入图片描述


可以进一步简化为:

class Solution {
    public int numIslands(char[][] grid) {
    	if(grid==null||grid.length==0)	return 0;
    	int[][] tmp=new int[grid.length][grid[0].length];
    	
    	int cnt=0;
    	for(int i=0;i<grid.length;i++) {
    		for(int j=0;j<grid[0].length;j++) {
    			if(grid[i][j]=='1'&&tmp[i][j]!=1)
    				cnt+=findRemain(i,j,grid,tmp);
    		}
    	}
    	
    	return cnt;
    }
    
    public int findRemain(int row,int col,char[][] grid,int[][] tmp) {
    	tmp[row][col]=1;
    	if(row!=0) {
    		if(grid[row-1][col]=='1'&&tmp[row-1][col]==0) {
    			tmp[row-1][col]=1;
    			findRemain(row-1,col,grid,tmp);
    		}
    	}
    	
    	if(col!=0) {
    		if(grid[row][col-1]=='1'&&tmp[row][col-1]==0) {
    			tmp[row][col-1]=1;
    			findRemain(row,col-1,grid,tmp);
    		}
    	}
    	
    	if(row!=grid.length-1) {
    		if(grid[row+1][col]=='1'&&tmp[row+1][col]==0) {
    			tmp[row+1][col]=1;
    			findRemain(row+1,col,grid,tmp);
    		}
    	}
    	
    	if(col!=grid[0].length-1) {
    		if(grid[row][col+1]=='1'&&tmp[row][col+1]==0) {
    			tmp[row][col+1]=1;
    			findRemain(row,col+1,grid,tmp);
    		}
    	}
    	return 1;
    }
}

看完题目评论区的答案,发现有些可以优化的地方:

1.不使用“check”“findRemain”这种函数名,既然是利用递归的算法,可以使用DFS作为子函数名;
2.findRemain中我分别探讨了上下左右的位置情况,这样代码写起来比较复杂,可以在findRemain开始的地方设置边界返回:

if(row<0||row==grid.length||col<0||col>grid[0].length)	return;

最终简化之后:

class Solution {
    public int numIslands(char[][] grid) {
    	if(grid==null||grid.length==0)	return 0;
    	int[][] tmp=new int[grid.length][grid[0].length];
    	
    	int cnt=0;
    	for(int i=0;i<grid.length;i++)
    		for(int j=0;j<grid[0].length;j++)
    			if(grid[i][j]=='1'&&tmp[i][j]!=1)
    				cnt+=DFS(i,j,grid,tmp);
    	return cnt;
    }
    
    public int DFS(int row,int col,char[][] grid,int[][] tmp) {
    	if(row<0||col<0||row==grid.length||col==grid[0].length)	return 0;
    	if(grid[row][col]=='0'||tmp[row][col]==1)	return 0;
    	tmp[row][col]=1;
    			DFS(row-1,col,grid,tmp);
    			DFS(row,col-1,grid,tmp);
    			DFS(row+1,col,grid,tmp);
    			DFS(row,col+1,grid,tmp);
    	return 1;
    }
}

运行结果:
在这里插入图片描述

淘系商品的成长密码:历史与趋势透视 本文分析了淘系商品的发展历程,从早期的市场探索到如今的行业领先地位,深入探讨了技术创新、供应链优化、用户需求洞察等关键因素在淘系商品成功中的作用。通过对平台不断完善商品生态、提升购物体验和服务的过程分析,文章总结了淘系商品在竞争激烈的市场中如何通过创新应对挑战并推动电商行业的进步。未来,随着消费趋势变化和新兴模式的兴起,淘系商品将在更大范围内发挥其市场影响力。 阅读详情

相关推荐

Ubuntu 24.04 部署 EMQX 5.8 开源版MQTT服务器:从零搭建到实战应用

本文详细指导如何在Ubuntu 24.04系统上部署EMQX 5.8开源版MQTT服务器。内容涵盖从环境准备、安装Erlang、下载安装包到启动服务的完整步骤,并提供了验证服务、配置网络防火墙以及通过WebSocket工具进行基础消息收发的实战方法,帮助用户快速搭建稳定、免费的物联网消息中间件。

weixin_29209825的博客 300

A. Medium Number

解题说明:水题,三个数字比大小即可。

无知的我 841

ES快速入门,一篇就懂!

Elastic Search 入门前言什么是搜索为什么常用数据库不适合做搜索什么是Lucene什么是Elastic SearchES核心概念ES使用正常启动索引​增删改查ES增加常见问题:ES删除ES查询欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的

qq1592的博客 9316

Codeforces Round #835 (Div. 4)题解

Codeforces Round #835 (Div. 4)个人题解

keguaiguai的博客 590

Redis【实战篇】---- 分布式锁-redission

重入问题:重入问题是指 获得锁的线程可以再次进入到相同的锁的代码块中,可重入锁的意义在于防止死锁,比如HashTable这样的代码中,他的方法都是使用synchronized修饰的,假如他在一个方法内,调用另一个方法,那么此时如果是不可重入的,不就死锁了吗?所以可重入锁他的主要意义是防止死锁,我们的synchronized和Lock锁都是可重入的。不可重试:是指目前的分布式只能尝试一次,我们认为合理的情况是:当线程在获得锁失败后,他应该能再次尝试获得锁。

是微风,是晚霞,是无可替代 1万+

「PAT乙级真题解析」Basic Level 1034 有理数四则运算 (问题分析+完整步骤+伪代码描述+提交通过代码)

题设给定两个有理数, 要求输出这两个有理数的和、差、积、商。 如果只是这样, 则问题很简单, 但是题设要求输出的每个有理数必须是最简形式. **所以本题的一个重点是如何将有理数转为题设要求的有理数.**

过美好生活,做高效程序员 3134

200. Number of Islands [Medium] DFS

200. Number of Islands 200. Number of Islands 一看就是深度优先的遍历 class Solution(object): def numIslands(self, grid): """ :type grid: List[List[str]] :...

qq_35319215的博客 162

【低照度图像增强系列】使用MSRCR(Multi-Scale Retinex with Color Restoration)算法对低照度图像进行增强处理

【低照度图像增强系列】使用MSRCR(Multi-Scale Retinex with Color Restoration)算法对低照度图像进行增强处理

静谧、淡雅 661

179. Largest Number-medium

最开始的思路:把所有数,按照位数最长的数的位数来补齐(补上的数即是该数的首位) 例如: 最长位数为5,则9补齐后是“99999”, 32补齐后是“32333”按照补齐后的数的大小来排序,做组合以上解法是找规律得出的解决的特殊情况: “121”和”12”,补足后,都是121,此例中应选12+121 0, 0 8308, 830,此例中应选8308+830问题是:上述思路,无法解决”121”和

linwutao0810的专栏 388

DFIG风电场经LCC-HVDC外送系统SIMULINK建模与仿真

本文基于MATLAB/Simulink平台构建了DFIG风电场经LCC-HVDC外送系统的仿真模型。系统采用1000MW聚合DFIG模型与双极12脉动LCC-HVDC连接,详细阐述了参数设计、控制策略及接口协调方法。重点解决了风电场无功能力不足、功率波动影响换相等关键技术问题,为大规模风电基地外送提供了仿真研究框架。模型整合了官方DFIG和HVDC详细模块,通过参数缩放和控制系统优化,实现了完整系统的动态仿真分析。

huanghm88的专栏 124

Vue - 超强实现网页禁止浏览器缩放功能,浏览器缩放时保持网页不变形、元素不会被挤压等,屏蔽浏览器鼠标滚轮缩放页面(提供详细示例代码,一次引入整个网站无人可以搞乱你的页面布局及样式)

vue,nuxt,禁止浏览器缩放,如何用户缩放浏览器导致页面变形,网页缩放后页面变形了怎么办,怎么解决网站被缩放后元素乱套错乱,vue怎么禁止网页不被缩放,vue如何禁止浏览器缩放,vue缩放网页变形影响布局怎么办,vue电脑分辨率及缩放导致页面变形的问题,前端页面解决浏览器或者系统被放大和缩小导致的显示问题,Vue中页面自适应缩放,如何防止Vue页面缩放时变形/调整大小,Vue项目中,防止页面被缩放和放大,vue等比例缩放网页,网页缩放后界面变形咋办,禁止页面缩放,vue禁止浏览器改变窗口大小,Vue项目

🏆 前端领域优质创作者 1万+

[Leetcode 200, medium] Number of Islands

[Leetcode 200, medium] Number of Islands

算法的实践 511

【网络安全】浅谈数据库攻击复现及相关安全优化

4、准备数据库服务器为了安全正式环境应该把数据库单独放一台服务器,只允许让应用服务器 IP 来访问数据库服务器,通过独立两者,就算其中一台发生问题,另一台也是好的,进一步降低了危险性。5、定时备份数据库开发定时备份功能,可以使用 Crontab,或者其他框架,设定周期备份数据,哪怕是数据丢失或被删除也可以快速恢复。

2401_88545587的博客 229

[Leetcode 200, Medium] Number of Islands

[Leetcode 200, Medium] Number of Islands

算法的实践 520

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【leetcode】【medium】200. Number of Islands​​​​​​​

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

lemonade13的博客 114

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【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

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

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的博客 343

【LeetCode】200. Number of Islands[Medium]

题目链接:岛屿数量 题目描述: 一个由'1'(陆地)和 '0'(水)组成的的二维网格,计算网格中岛屿的数量。 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。 此外,可以假设该网格的四条边均被水包围。 分析:一个值为‘1’的网格,和其上下左右值为‘1’的网格,处于一个岛屿。 所以要进行遍历,对于每一个值为‘1’的网格,访问以其为中心的,周围能够到达的所有值为‘1’的网格,那么这些能够连通的网格处于一个岛屿。因为一个网格只能处于一个岛屿,为了防止一个网格被重复访问.

Greenary的博客 354

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

【LeetCode】 200. Number of Islands 岛屿数量(Medium)(JAVA) 题目地址: 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 i...

qq_16927853的博客 507

[leetcode] 200. Number of Islands (Medium)

原题 0代表水,1代表陆地,边界土地的上下左右不相连其他土地,则称为一块陆地。 计算有几块陆地。 思路: DFS,从起点开始,每碰到一个‘1’,就作为DFS起点,将其相近的‘1’全部变为‘0’。直到无‘1’。 一开始dfs没有传参数组长度,而是每一次都调用size(),速度很慢 Runtime: 12 ms, faster than 47.97% 修改以后 Runtime: 8 ms, faste...

Rh's blog 177

leetcode 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 by water and is formed by connecting adjacent lands horizontally or vertically. You may assume...

weixin_30588907的博客 133

LeetCode 200.Number of Islands(岛屿数量) 并查集/medium

文章目录1.Description2.Example3.Solution 1.Description 给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。 此外,你可以假设该网格的四条边均被水包围。 2.Example 输入:grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"],

押切徹的博客 225

[LeetCode] 200. Number of Islands_ Medium tag: 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...

weixin_30677475的博客 72

Leetcode: 200. Number of Islands (week4 --- 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 connecting adjacent lands horizontally or v...

MJ_GOD 348

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

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的博客 482
上一篇: 【vue笔记】调试日志
下一篇: 【leetcode】199.(Medium) Binary Tree Right Side View
AXIMI
博客等级 码龄10年 53粉丝 455原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值