[LeetCode] 0200. 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.
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
解法一
思路:逐元素遍历输入的二维数组,遇到1时展开BFS搜索,并用记录下遇到过的节点。
参考资料:https://www.cnblogs.com/grandyang/p/4402656.html
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if (grid.empty() || grid[0].empty()) {
return 0;
}
int n_row = grid.size();
int n_col = grid[0].size();
int res = 0;
vector<vector<bool>> visited(n_row, vector<bool>(n_col));
vector<int> dirX{-1, 0, 1, 0}, dirY{0, 1, 0, -1};
for (int i = 0; i < n_row; i++) {
for (int j = 0; j < n_col; j++) {
if (grid[i][j] == '0' || visited[i][j])
continue;
++res;
queue<int> q{{i * n_col + j}};
while (!q.empty()) {
int t = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
int x = t / n_col + dirX[k];
int y = t % n_col + dirY[k];
if (x < 0 || x >= n_row || y < 0 || y >= n_col || grid[x][y] == '0' || visited[x][y])
continue;
visited[x][y] = true;
q.push(x * n_col + y);
}
}
}
}
return res;
}
};
[LeetCode] 0200. Number of Islands 岛屿的个数的更多相关文章
- [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]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 200. Number of Islands 岛屿数量(C++/Java)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- 200 Number of Islands 岛屿的个数
给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
- [LeetCode] 305. Number of Islands II 岛屿的数量 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 ...
随机推荐
- phpspreadsheet 中文文档(七)技巧和诀窍
2019年10月11日14:08:35 以下页面为您提供了一些使用广泛的PhpSpreadsheet食谱.请注意,这些文件没有提供有关特定PhpSpreadsheet API函数的完整文档,而只是一个 ...
- Caused by java.lang.Exception Failed to send data to Kafka Expiring
flink 写kafka,报错,作业挂掉 Caused by: java.lang.Exception: Failed to send data to Kafka: Expiring 89 recor ...
- IIS配置实现反向代理(图文)
需求: 网站在备案,本来的网站不符合要求,先反向到别的网站.原网站:test.com, 目标网站:target.com 设置反向代理的服务器一定是在原网站服务器上. 注意:iis应该是iis7及以上版 ...
- PHP写接口需要注意的问题
PHP写接口需要注意的问题方法前面建议写传过来的参数 1 防止xss攻击2 防止sql语句注入3 sign签名验证4 不经常更新的加索引和缓存 app请求时候 参数都要加urlencode 防止参数带 ...
- Maven依赖中scope的含义
https://www.jianshu.com/p/7145f01ac3ad Maven依赖中scope的含义 整理一下Maven中Scope的详细作用,都是抄的别人内容整理了一下.参考: https ...
- [转帖]Proof Of Work 工作量证明
Proof Of Work 工作量证明 https://www.cnblogs.com/zhang-qc/p/10451817.html 借鉴了 哈希现金(Hashcash)-1997年 英国密码学专 ...
- day15——有参装饰器、多个装饰器装饰一个函数
day15 装饰器的进阶 有参装饰器 @auth(chose) 相等于以下两行代码的解构 wrapper = auth(chose) foo = wrapper(foo) # dic = {'user ...
- [终极巨坑]golang+vue开发日记【二】,登陆界面制作(一)
写在前面 本期内容是适合第一次使用vue或者golang开发的,内容会以实战的形式来讲解.看懂本段内容需要了解基础内容有html,css,最好可以看一下vue的基础.并且这里的每个知识点不可能详细解说 ...
- Go grpc 与 protobuf
现在很多微服务内部的通信协议都采用rpc,性能高,安全.而grpc则是google退出的rpc plus. protobuf是传输协议,性能高,强大. 来一个server client的通信demo, ...
- cocos creator ScrollView组件scrollToOffset()方法的使用
前言 之前想用scrollToOffset()在打开界面时,滑动窗口滑动到一个相对应的位置,但是使用scrollToOffset()这个方法的时候,没起作用.然后就用了其他方法来实现相同的效果.现在有 ...