岛屿的个数

给一个01矩阵,求不同的岛屿的个数。

0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。

您在真实的面试中是否遇到过这个题?

Yes
样例

在矩阵:

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

中有 3 个岛.

 class Solution {
public:
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
// Write your code here
if(grid.empty() || grid[].empty())
return ;
int m = grid.size(), n = grid[].size();
int count = ;
for(int i = ; i < m; ++i){
for(int j = ; j < n; ++j){
if(grid[i][j]){
++count;
dfs(grid, i, j);
}
}
}
return count;
}
void dfs(vector<vector<bool> > &grid, int x, int y){
if((x < ) || (y < ) || (x >= grid.size()) || (y >= grid[].size()) || !grid[x][y])
return;
grid[x][y] = false;
dfs(grid, x - , y);
dfs(grid, x, y - );
dfs(grid, x + , y);
dfs(grid, x, y + );
}
};

lintcode433 岛屿的个数的更多相关文章

  1. lintcode:Number of Islands 岛屿的个数

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

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

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

  4. leetcode 岛屿的个数 python

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

  5. 岛屿的个数12 · Number of Islands12

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

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

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

  8. LintCode 433. 岛屿的个数(Number of Islands)

    LintCode 433. 岛屿的个数(Number of Islands) 代码: class Solution: """ @param grid: a boolean ...

  9. LeetCode200 岛屿的个数

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

随机推荐

  1. C#强大的编程功能

    下面列出一些C#重要的功能 1.布尔条件 2.自动垃圾回收 3.标准库 4.组件版本 5.属性和事件 6.委托和事件管理 7.易于使用的泛型 8.索引器 9.条件编译 10.简单的多线程 11.LIN ...

  2. tomcat文件夹下各文件夹的作用

    1.bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(windows命令). 很多环境变量的设置都在此处. 2.conf目录主 ...

  3. 架构风格:你真的懂REST吗?

    本文探讨如下几个问题: 什么是REST REST包含哪些约束 什么是RESTful 纯RESTful API的难点在哪里 如果你去搜索「什么是REST」的话,大部分情况下,你看到的基本都是RESTfu ...

  4. Maven常用的构建命令

    1.mvn -v 查看maven版本 2.mvn compile 编译项目,生成target文件夹,其中包含编译生成的字节码文件和测试报告.打开cmd,cd到项目的根目录,运行该命令如图所示(如果是第 ...

  5. oc中文首字母排序

    oc中文首字母排序 NSArray *arr2=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛& ...

  6. 数据库函数(Left、Right)

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr()  ...

  7. 关于端口冲突的解决方式Error: listen EACCES 0.0.0.80

    笔者昨天下午临走前安装了vs 2017想要运行一下项目的NET后端来让本机的前端直接对接后端,但是没注意到运行vs后IIS直接占用了本机的80端口.第二天跑nodeJS的时候直接Error: list ...

  8. Pagination

    using System.Collections.Generic; namespace Oyang.Tool { public interface IPagination { int PageInde ...

  9. 【Spark】Spark2.x版的新特性

    一.API 1. 出现新的上下文接口:SparkSession,统一了SQLContext和HiveContext,并且为SparkSession开发了新的流式调用的configuration API ...

  10. 谈谈php对象的依赖

    通过构造函数的方法 <?php //定义一个类,后面的类依赖这个类里面的方法 class play { public function playing() { echo "I can ...