当前位置:网站首页>200. 岛屿数量
200. 岛屿数量
2022-04-22 11:11:00 【不爱研究的研究僧】
题目:
给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
题解:

力扣 将岛屿问题讲的很清楚。
class Solution {
public int numIslands(char[][] grid) {
int res = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
//如果是陆地就进行dfs,然后岛屿数量加1
//如果遍历到标记2的地方,说明是之前的岛屿扩展的,不需要dfs,也不需要岛屿数量加1
if (grid[i][j] == '1') {
dfsGrid(grid, i, j); //遍历四周,对遍历过的陆地位置做标记 2
res++;
}
}
}
return res;
}
//遍历四周,对遍历过的陆地位置做标记 2
public void dfsGrid(char[][] grid, int row, int col) {
if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) { //超出边界就返回
return;
}
if (grid[row][col] != '1') { //如果是0或者2就返回
return;
}
grid[row][col] = '2'; //遍历过的陆地位置做标记
//上下左右四个方向的dfs
dfsGrid(grid, row + 1, col);
dfsGrid(grid, row - 1, col);
dfsGrid(grid, row, col + 1);
dfsGrid(grid, row, col - 1);
}
}
版权声明
本文为[不爱研究的研究僧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43955488/article/details/124337942
边栏推荐
- Looking for investors of state-owned enterprises, central enterprises and listed companies, I choose Tami dog!
- 110T oracle故障恢复
- MySql5. 7.26 installation
- 2022 zhongkepan cloud - WB security application flag
- How to solve this problem when installing MySQL?
- MySQL installation summary
- CISSP certified daily knowledge points (April 20, 2022)
- Best practices of Apache APIs IX high availability configuration center based on tidb
- P100 - clue penetration test using searchsploit
- 接口自动化-Session鉴定解决方案
猜你喜欢

阿里超大规模 Flink 集群运维体系介绍

ES6 learning notes 4 numerical representation related to numerical expansion (octal representation) (reprinted, the memo is not my original)

MySQL安装总结

Redis 环境安装

Interface normative test standard specification - detailed

机器学习基础

Postman interface testing tool video tutorial, zero basic entry to master graduation

MySQL startup failure: the MySQL service cannot be started. The service did not report any errors. Solution

观测云入驻阿里云计算巢,为用户构建稳定安全的云上连接

重点来了,最全Web3行话指南你掌握了吗?
随机推荐
Ampere computing releases the computing power of observation cloud "core" and jointly promotes the development of observability
uboot目录结构分析
Low sidelobe weighting processing of phased array antenna
华为云IoT专家团张俭:22岁就已成为华为高级工程师,代码是我对这个世界想说的话
MySQL latest version 8.0.21 installation and configuration tutorial~
Chat chat app Lesson 6 creating a main chat static page
FinBI连接本机mysql
软件测试基础知识整理(详细版)收藏这篇足矣
Menu bar, toolbar and status bar of QT
DevSecOps软件研发安全实践——设计篇
CISSP certified daily knowledge points (April 20, 2022)
vulnhub The Planets: Earth
Summary of seven design principles
Fundamentals of machine learning
Analysis of module d electronic answer card of "network security" (secondary vocational group) in 2022 skill competition - new vulnerabilities
Qdebug() print debugging information
原来,这才是开发者打开世界读书日的正确姿势!
13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
十年测试经验,我整理出了最适合你的软件测试学习指南
关于正则表达式匹配密码问题