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.

Have you met this question in a real interview?

Yes
Example

Given graph:

  1. [
  2. [1, 1, 0, 0, 0],
  3. [0, 1, 0, 0, 1],
  4. [0, 0, 0, 1, 1],
  5. [0, 0, 0, 0, 0],
  6. [0, 0, 0, 0, 1]
  7. ]

return 3.

LeetCode上的原题,请参见我之前的博客Number of Islands

  1. class Solution {
  2. public:
  3. /**
  4. * @param grid a boolean 2D matrix
  5. * @return an integer
  6. */
  7. int numIslands(vector<vector<bool>>& grid) {
  8. if (grid.empty() || grid[].empty()) return ;
  9. int m = grid.size(), n = grid[].size(), res = ;
  10. vector<vector<bool>> visited(m, vector<bool>(n, false));
  11. for (int i = ; i < m; ++i) {
  12. for (int j = ; j < n; ++j) {
  13. if (grid[i][j] && !visited[i][j]) {
  14. helper(grid, visited, i, j);
  15. ++res;
  16. }
  17. }
  18. }
  19. return res;
  20. }
  21. void helper(vector<vector<bool>>& grid, vector<vector<bool>>& visited, int i, int j) {
  22. int m = grid.size(), n = grid[].size();
  23. if (i < || i >= m || j < || j >= n || !grid[i][j] || visited[i][j]) return;
  24. visited[i][j] = true;
  25. helper(grid, visited, i + , j);
  26. helper(grid, visited, i - , j);
  27. helper(grid, visited, i, j + );
  28. helper(grid, visited, i, j - );
  29. }
  30. };

[LintCode] Number of Islands 岛屿的数量的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LintCode] Number of Islands(岛屿个数)

    描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...

  4. 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 ...

  5. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  6. [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 ...

  7. [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 ...

  8. 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...

  9. Leetcode200. Number of Islands岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

随机推荐

  1. 用C获得当前系统时间(转)

    #include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...

  2. java的分层开发

    既然是分层开发,首先我们需要知道的是分为那几个层,并且是干什么的? 1.实体层(entity) 对应数据库中的一张表,有了它可以降低耦合性,同时也是数据的载体. 2.数据访问对象(data acces ...

  3. C++ 拷贝构造函数 和 六大函数

    1.  C++什么时候会调用 拷贝构造函数? a.一个对象作为函数参数,以值传递的方式传入函数体: b.一个对象作为函数返回值,以值传递的方式从函数返回:(实际使用时,会被编译器优化掉) c.一个对象 ...

  4. HashMap合并相同key的value

    Map<String, String> map1 = new HashMap<>(); map1.put("x", "y"); map1 ...

  5. 【转】Kylin的Hierarchies,Derived维度方面配置优化

    http://blog.csdn.net/jiangshouzhuang/article/details/51286150 Hierarchies: 理论上对于N维度,我们可以进行2的N次方的维度组合 ...

  6. Linux系统性能10条命令监控

    Linux系统性能10条命令监控 概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 p ...

  7. vim 分屏功能

    分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 file2 ... 注释: n是数字,表示分成几 ...

  8. Codeforces Round #354 (Div. 2)-A

    A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...

  9. zepto下加载openbox弹出层

    function fnOpenBox(objId,animateD,speed,tween,hide){ var oOpenBox = $(objId); oOpenBox.show(); oOpen ...

  10. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...