当前位置:网站首页>LeetCode_ DFS_ Medium_ 200. Number of islands
LeetCode_ DFS_ Medium_ 200. Number of islands
2022-04-22 12:18:00 【A scoop of Jianghu me ups and downs】
1. 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 edges of the mesh are surrounded by water .
Example 1:
Input :grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output :1
Example 2:
Input :grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output :3
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] The value of is ‘0’ or ‘1’
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/number-of-islands
2. Ideas
(1)DFS
Train of thought reference Kill all the islands in a second .
3. Code implementation (Java)
// Ideas 1————DFS
class Solution {
public int numIslands(char[][] grid) {
//res Save the number of Islands , The initial value is 0
int res = 0;
int m = grid.length;
int n = grid[0].length;
// Traverse grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
// Find an island
res++;
// Use DFS Submerge the island ( Including the adjacent land up, down, left and right )
dfs(grid, i, j);
}
}
}
return res;
}
public void dfs(char[][] grid, int i, int j) {
int m = grid.length;
int n = grid[0].length;
// Judge boundary condition
if (i < 0 || j < 0 || i >= m || j >= n) {
return;
}
if (grid[i][j] == '0') {
// The current position is already sea water , Then return directly
return;
}
// Change the current position to sea water , Equivalent to using an array visited Record the traversed nodes
grid[i][j] = '0';
// At the same time, it also submerges the land up, down, left and right
dfs(grid, i - 1, j);
dfs(grid, i + 1, j);
dfs(grid, i, j - 1);
dfs(grid, i, j + 1);
}
}
版权声明
本文为[A scoop of Jianghu me ups and downs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221214183368.html
边栏推荐
- Heap overflow of kernel PWN basic tutorial
- 电工第一讲
- Smart business card applet creates business card page function and realizes key code
- 基于J2EE的房屋租赁系统的设计与实现.rar(论文+项目源码+数据库文件)
- What does 0ul or 1ul in STM32 mean?
- base64加密解密和json处理
- Best buy website EDI test process
- NER简单综述
- Cas 4 - 1.7: transfert de fichiers (et recherche d'ensembles)
- Base64 encryption, decryption and JSON processing
猜你喜欢

js 【详解】闭包
![[deeply understand tcallusdb technology] insert data into the specified location of the list interface description - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[deeply understand tcallusdb technology] insert data into the specified location of the list interface description - [list table]

Efr32 crystal calibration guide
![[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]

开发者友好型公链Neo | 如何连接 Web2 开发者到 Web3 世界

LeetCode 206、反转链表

Low frequency (LF) RFID intelligent terminal
![[in depth understanding of tcallusdb technology] data interface description for reading the specified location in the list - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] data interface description for reading the specified location in the list - [list table]

C语言%7.2d、%-7d、%7.2f、%0.2f的含义

Smart business card applet creates business card page function and realizes key code
随机推荐
Remember a study of Intranet penetration into the shooting range
电工第一讲
ESP32-CAM使用历
What is the difference between CPU and GPU?
Comparison of data protection modes between Oracle data guard and Jincang kingbasees cluster
【并发编程054】多线程的状态及转换过程?
【keras入门】MNIST数据集分类
Setting policy of thread pool size
MySQL 5.0安装教程图解详细教程
电路实验——实验四 戴维南定理与诺顿定理
论文阅读《Attention Concatenation Volume for Accurate and Efficient Stereo Matching》
How can fresh graduates break through the encirclement of internet job hunting
Best buy website EDI test process
Ptorch quantification (torch. Quantification)
MySQL学习第四弹——多表查询分类以及案例练习源码详解
congratulations! You have been concerned about the official account for 1 years, and invite you to join NetEase data analysis training.
Where have all the Internet people who left their jobs gone?
Case 4-1.7: file transfer (concurrent search)
【深入理解TcaplusDB技术】更替列表指定位置数据接口说明——[List表]
What kind of SQL report can use the max – Pt function?