当前位置:网站首页>leetcode 200. Number of islands
leetcode 200. Number of islands
2022-06-27 16:58:00 【chenyson】
difficulty : secondary
The frequency of :122
subject : Here you are ‘1’( land ) and ‘0’( water ) A two-dimensional mesh of , Please calculate the number of islands in the grid .
Islands are always surrounded by water , And each island can only be combined by horizontal direction / Or a vertical connection between adjacent lands .
Besides , You can assume that all four sides of the mesh are surrounded by water .
Their thinking : Depth-first search
Be careful :
- DFS It is necessary to judge whether it is still in the matrix ( Jump out of bounds DFS) And whether that point is land ( If the ocean jumps out DFS)
- Depth-first search , You need to avoid repeated iterations ( Mark the passing become 0)
- Two are required in the main function for, Do a two-dimensional traversal
- row=grid.length;col=grid[0].length
Code
class Solution {
public int numIslands(char[][] grid) {
//row The length of the line col Column length
int r=grid.length;
int c=grid[0].length;
int count=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
// If it's land , it DFS, Jump out by the way DFS In time COUNT++
if(grid[i][j]=='1'){
DFS(grid,i,j);
count++;
}
}
}
return count;
}
// You don't need to return anything , So it's void
public void DFS(char[][] grid,int r,int c){
// Judge (r,c) Whether in grid in , Out of or for the sea
if(r<0||c<0||r>=grid.length||c>=grid[0].length) return;
// Here are two steps to take apart , If put together , There may be a mistake . Is due to || It's the nature of
if(grid[r][c]=='0') return;
grid[r][c]='0';
// On
DFS(grid,r-1,c);
// Next
DFS(grid,r+1,c);
// Left
DFS(grid,r,c-1);
// Right
DFS(grid,r,c+1);
}
}
边栏推荐
- A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe
- Hierarchical clustering and case analysis
- Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
- A large number of missing anchor text
- Realize simple three-D cube automatic rotation
- Event listening mechanism
- 2022年中国音频市场年度综合分析
- Regular matching starts with what, ends with what, starts with what, and ends with what
- P.A.R.A 方法在思源的简易应用(亲测好用)
- [pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
猜你喜欢

d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?

Unity 阴影——阴影平坠(Shadow pancaking)

Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)

List to table

d3dx9_ What if 27.dll is lost? d3dx9_ How to repair 27.dll?

数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】

Popularization of MCU IO port: detailed explanation of push-pull output and open drain output

10 minutes to master the installation steps of MySQL

Missing d3d10 How to repair DLL files? Where can I download d3d10.dll

Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
随机推荐
Adaoracle supports multi chain distributed Oracle with wide area node quotation
事件监听机制
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
字节跳动埋点数据流建设与治理实践
The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
2022年中国音频市场年度综合分析
Ping An technology's practice of migrating from Oracle to ubisql
Ten common methods of arrays tools
Data center table reports realize customized statistics, overtime leave summary record sharing
Cesium realizes satellite orbit detour
关于VS2019C#如何建立登陆界面输入的用户名和密码需与Access数据库的记录相匹配
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
How to modify / display GPIO status through ADB shell
阿里云刘珅孜:云游戏带来的启发——端上创新
Unity 阴影——阴影平坠(Shadow pancaking)
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Redis系列2:数据持久化提高可用性
P. Simple application of a.r.a method in Siyuan (friendly testing)
Event listening mechanism
Raspberry pie preliminary use