LeetCode200 岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入:
11110
11010
11000
00000 输出: 1
示例 2:
输入:
11000
11000
00100
00011 输出: 3
//章节 - 队列和栈
//二、队列与广度优先搜索
//1.岛屿的个数
/*
算法思想:
这是一道很有意思的题,本质是求矩阵中连续区域的个数,很容易想到需要用深度优先搜索DFS来解,需要建立一个visited数组用来记录某个位置是否被访问过,对于一个为‘1’且未被访问过的位置,递归进入其上下左右位置上为‘1’的数,将其visited对应值赋为true,继续进入其所有相连的邻位置,这样可以将这个连通区域所有的数找出来,并将其对应的visited中的值赋true,找完一次区域后,我们将结果res自增1,然后我们在继续找下一个为‘1’且未被访问过的位置,以此类推直至遍历完整个原数组即可得到最终结果。
*/
//算法实现:
class Solution {
public:
int numIslands(vector<vector<char> > &grid) {
if (grid.empty() || grid[0].empty()) //
return 0;
int m = grid.size(), n = grid[0].size(), res = 0; //m行n列
vector<vector<bool> > visited(m, vector<bool>(n, false)); //将二维数组初始为false,即都未访问
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1' && !visited[i][j]) { //为1且未被访问,则递归进入其上下左右位置上为‘1’的数
numIslandsDFS(grid, visited, i, j);
++res; //找完一次区域后,结果res自增1
}
}
}
return res;
}
void numIslandsDFS(vector<vector<char> > &grid, vector<vector<bool> > &visited, int x, int y) {
if (x < 0 || x >= grid.size())
return;
if (y < 0 || y >= grid[0].size())
return;
if (grid[x][y] != '1' || visited[x][y]) //不为1或已被访问
return;
visited[x][y] = true; //已访问
numIslandsDFS(grid, visited, x - 1, y); //递归访问上位置
numIslandsDFS(grid, visited, x + 1, y); //递归访问下位置
numIslandsDFS(grid, visited, x, y - 1); //递归访问左位置
numIslandsDFS(grid, visited, x, y + 1); //递归访问右位置
}
};
LeetCode200 岛屿的个数的更多相关文章
- [Swift]LeetCode200.岛屿的个数 | 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 ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] Number of Distinct Islands 不同岛屿的个数
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- leetcode 岛屿的个数 python
岛屿的个数 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包 ...
- 岛屿的个数12 · Number of Islands12
[抄题]: 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. [ [1, 1, 0, 0, 0], [0, 1, 0, 0 ...
- lintcode433 岛屿的个数
岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 您在真实的面试中是否遇到过这个题? Yes 样例 在矩阵: ...
- [LeetCode] 711. Number of Distinct Islands II 不同岛屿的个数之二
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] 694. Number of Distinct Islands 不同岛屿的个数
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
随机推荐
- Algorithm homework 1
一.已知下列递推式: \[C(n)= \begin{cases} 1 & , & n = 1 \\ 2C(n/2) + n - 1& , & n \geq 2 \end ...
- springboot配置ssl证书
springboot默认使用的是tomcat: 1.先到阿里云上注册一个证书,绑定域名:后面可以在管理中下载证书,下载tomcat对应的证书(一个*.pfx文件和*.txt文件) 2.将pfx文件拷贝 ...
- JavaSE基础面试题
1. Java语言有哪些特点(1)简单易学.有丰富的类库(2)面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高)(3)与平台无关性(JVM是Java跨平台使用的根本)(4)可靠安全(5)支 ...
- rpl_semi_sync_master_wait_no_slave 参数研究实验
最近在研究MySQL,刚学到半同步. 半同步的配置中,关于这两个参数: rpl_semi_sync_master_wait_no_slave rpl_semi_sync_master_wait_for ...
- GC agent的安装和卸载
一.GC agent安装 下面介绍GC agent的push和pull两种安装方法 1.push(推送)安装GC agent方法 1).打开EMGC home page:https://even.or ...
- 如何用Python 制作词云-对1000首古诗做词云分析
公号:码农充电站pro 主页:https://codeshellme.github.io 今天来介绍一下如何使用 Python 制作词云. 词云又叫文字云,它可以统计文本中频率较高的词,并将这些词可视 ...
- burpsuite 2020.12.1最新版蓝色版,下载安装破解
最新版Burpsuite下载破解安装 安装包链接:https://cloud.189.cn/t/qUVzYbuAJzqm(访问码:mw12) 1.首先,我们解压,可以看到解压后的文件夹里有如下文件 2 ...
- JavaWeb基础总结:Servlet专题
最近工作中有部分整改老接口的任务,大部分与Spring的拦截器,Tomcat相关,改到一些底层的代码发现,对基础J2EE的知识有些遗忘,需要频繁查阅,索性从头系统的整理一下Servlet和Filter ...
- linux中搭建phpmyadmin详细流程
一.phpmyadmin部署流程 1.1介绍 phpMyAdmin是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库. ...
- python初学者-计算1-99奇数的和
s = 0 for i in range(1,100,2): s = s + i print(s)