Number of Islands——LeetCode进阶路
原题链接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 connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
思路分析
这道题和上一题 “围绕的区域”https://blog.csdn.net/Moliay/article/details/88584035
思路相似,同用dfs,特定元素访问之后标记,递归出结果 bingo
源码附录
class Solution {
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0)
{
return 0;
}
int row = grid.length;
int col = grid[0].length;
int result = 0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(grid[i][j] == '1')
{
result ++;
dfs(grid,i,j,row,col);
}
}
}
return result;
}
public void dfs(char[][] grid,int i,int j,int row,int col)
{
if(i>row-1 || j>col-1 || i<0 || j<0)
{
return;
}
grid[i][j] = '2';
if(j<col-1 && grid[i][j+1] == '1')
{
dfs(grid,i,j+1,row,col);
}
if(j>0 && grid[i][j-1] == '1')
{
dfs(grid,i,j-1,row,col);
}
if(i<row-1 && grid[i+1][j] == '1')
{
dfs(grid,i+1,j,row,col);
}
if(i>0 && grid[i-1][j] == '1')
{
dfs(grid,i-1,j,row,col);
}
}
}
Number of Islands——LeetCode进阶路的更多相关文章
- Number of Islands——LeetCode
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for 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 surro ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [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 surro ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- MySQL - [07] 查看库表数据所使用的空间大小
1.切换数据库:use information_schema; 2.查看数据库使用大小 SELECT concat(round(sum(data_length/1024/1024),2),'MB') ...
- MySQL数据库datetime类型不能为空值的问题
修改mysql的配置文件:my.ini 将其只的: sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUT ...
- 『Python底层原理』--Python字典的实现机制
在Python中,字典(dict)是一种极为强大且常用的内置数据结构,它以键值对的形式存储数据,并提供了高效的查找.插入和删除操作. 接下来,我们将深入探究 Python 字典背后的实现机制,特别是其 ...
- GUI图形界面编程(Java)
GUI编程 组件 窗口 弹窗 面板 文本框 列表框 按钮 图片 监听事件 鼠标 键盘事件 破解工具 1.简介 Gui的核心技术:Swing.AWT 2.AWT 2.1.AWT介绍 1.包含了很多类和接 ...
- 【ABAQUS 二次开发笔记】一次获得多个积分点的输出到dat
当使用shell单元进行composite laminate 建模时,可以为每一指定Intergration point 的个数,默认是3个.(abaqus有很多variable可以在intergra ...
- QSound、QSoundEffect播放WAV音频
QSound.QSoundEffect播放WAV音频 本文旨在介绍QSound.QSoundEffect的简单播放音频的方法以及对这两个类的一些基本介绍 文章目录 QSound.QSoundEffec ...
- .tpl 是什么文件
介绍 .tpl 是一种文件扩展名,通常是指模板文件(template file). 模板文件是包含预定义格式和占位符变量的文本文件,用于生成其他文件或输出,如代码或配置文件. 一些常见的模板文件格式包 ...
- Delphi CheckListBox 用法
for i := CheckListBox1.Items.Count-1 downto 0 do //从后面往前面删 begin if CheckListBox1.Checked[i] then // ...
- 【python-数据分析】pandas时间序列处理
1. timestamp 1.1 创建timestamp 自定义timestamp 语法:pd.Timestamp(ts_input,tz,year,month,day,hour,minute,sec ...
- 谷歌SRE的7条原则
谷歌SRE的7条原则 拥抱合理的风险 最大化系统的稳定性不仅毫无意义,而且会适得其反.不切实际的可靠性目标限制了新功能交付给用户的速度,而且用户通常不会注意到极端的可用性(比如99.99999%),因 ...