岛屿的个数

给一个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. Google Guava -缓存cache简单使用

    package guavacache; import java.util.concurrent.ExecutionException; import java.util.concurrent.Time ...

  2. ARM 汇编指令集 特点之一:指令后缀

    1.同一 指令 添加不同的后缀 就会有不同的功能! 例子: B (Byte)  功能不变,操作长度变为8位 H (Half Word) 功能不变,操作长度变为16位 S(Signed) 功能不变,操作 ...

  3. C# abstract,virtual 修饰符

    abstract(抽象):该abstract修饰符指示要修改的东西有缺失或不完整的实现.abstract修饰符可以与类,方法,属性,索引器和事件一起使用.abstract在类声明中使用修饰符来指示类仅 ...

  4. 微信小程序获取手机信息

    wx.getSystemInfo({ success: function (res) { console.log(res.model)//手机型号 console.log(res.pixelRatio ...

  5. 课时25.a标签基本使用(掌握)

    什么是a标签? 我们打开百度或者淘宝网页,观察任何一个超链接都会发现它有如下特质: 有下划线 移上去有小手指 可以点击 a标签的作用:就是用于控制页面与页面之间跳转的 a标签的格式:<a hre ...

  6. C++ map练习

    C++ STL之map map介绍 C++里的map数据结构,会存储键值对信息key-value,通过key得到value的信息.map的key与value有一个特点就是:每个唯一的key拥有唯一对应 ...

  7. HDU 2065 "红色病毒"问题(生成函数)

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. Windows系统常见问题

    1.Windows自动更新灰色不能修改HKEY_LOCAL_MACHINE/Software/Policies/Microsoft/WindowsWindowsUpdate的资料夹,在WindowsU ...

  9. ckeditor + ckfinder + oss存储

    ckeditor 与 ckfinder 的整合方法 网上有很多,这里我也就不说了.  (主要是以前整合的现在忘记咋弄的了0.0) 我这里整合后直接使用js代码 <script type=&quo ...

  10. STM32 硬件UART接收超时检测设置

    STM32 硬件UART接收超时检测设置 -----------------本文作者"智御电子",期待与电子爱好者交流学习.---------------- 应用场景 在uart应 ...