[LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands.
Notice
0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.
Given graph:
[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]
return 3
.
LeetCode上的原题,请参见我之前的博客Number of Islands。
- class Solution {
- public:
- /**
- * @param grid a boolean 2D matrix
- * @return an integer
- */
- int numIslands(vector<vector<bool>>& grid) {
- if (grid.empty() || grid[].empty()) return ;
- int m = grid.size(), n = grid[].size(), res = ;
- vector<vector<bool>> visited(m, vector<bool>(n, false));
- for (int i = ; i < m; ++i) {
- for (int j = ; j < n; ++j) {
- if (grid[i][j] && !visited[i][j]) {
- helper(grid, visited, i, j);
- ++res;
- }
- }
- }
- return res;
- }
- void helper(vector<vector<bool>>& grid, vector<vector<bool>>& visited, int i, int j) {
- int m = grid.size(), n = grid[].size();
- if (i < || i >= m || j < || j >= n || !grid[i][j] || visited[i][j]) return;
- visited[i][j] = true;
- helper(grid, visited, i + , j);
- helper(grid, visited, i - , j);
- helper(grid, visited, i, j + );
- helper(grid, visited, i, j - );
- }
- };
[LintCode] Number of Islands 岛屿的数量的更多相关文章
- [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 ...
- [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 ...
- [LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...
- 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 ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 su ...
- [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 ...
- 200 Number of Islands 岛屿的个数
给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
随机推荐
- 用C获得当前系统时间(转)
#include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...
- java的分层开发
既然是分层开发,首先我们需要知道的是分为那几个层,并且是干什么的? 1.实体层(entity) 对应数据库中的一张表,有了它可以降低耦合性,同时也是数据的载体. 2.数据访问对象(data acces ...
- C++ 拷贝构造函数 和 六大函数
1. C++什么时候会调用 拷贝构造函数? a.一个对象作为函数参数,以值传递的方式传入函数体: b.一个对象作为函数返回值,以值传递的方式从函数返回:(实际使用时,会被编译器优化掉) c.一个对象 ...
- HashMap合并相同key的value
Map<String, String> map1 = new HashMap<>(); map1.put("x", "y"); map1 ...
- 【转】Kylin的Hierarchies,Derived维度方面配置优化
http://blog.csdn.net/jiangshouzhuang/article/details/51286150 Hierarchies: 理论上对于N维度,我们可以进行2的N次方的维度组合 ...
- Linux系统性能10条命令监控
Linux系统性能10条命令监控 概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 p ...
- vim 分屏功能
分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 file2 ... 注释: n是数字,表示分成几 ...
- Codeforces Round #354 (Div. 2)-A
A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...
- zepto下加载openbox弹出层
function fnOpenBox(objId,animateD,speed,tween,hide){ var oOpenBox = $(objId); oOpenBox.show(); oOpen ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...