lintcode-433-岛屿的个数
433-岛屿的个数
给一个01矩阵,求不同的岛屿的个数。
0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。样例
在矩阵:
[
[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 个岛.标签
脸书 谷歌 Zenefits
思路
使用深度优先遍历,如果某点未被遍历过,则表示它处于一个新岛屿上,此时遍历与其在同一岛屿上的点
code
class Solution {
public:
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
// Write your code here
int sizeRow = grid.size();
if (sizeRow <= 0) {
return 0;
}
int sizeCol = grid[0].size();
if (sizeCol <= 0) {
return 0;
}
int count = 0;
vector<vector<bool>> isVisited(sizeRow, vector<bool>(sizeCol, false));
for (int i = 0; i < sizeRow; i++) {
for (int j = 0; j < sizeCol; j++) {
if (grid[i][j] == true && !isVisited[i][j]) {
numIslands(i, j, grid, isVisited);
count++;
}
}
}
return count;
}
void numIslands(int i, int j, vector<vector<bool>> &grid, vector<vector<bool>> &isVisited) {
if (i < 0 || i >= grid.size()) {
return;
}
if (j < 0 || j >= grid[0].size()) {
return;
}
if (grid[i][j] == false || isVisited[i][j] == true) {
return;
}
isVisited[i][j] = true;
numIslands(i - 1, j, grid, isVisited);
numIslands(i + 1, j, grid, isVisited);
numIslands(i, j - 1, grid, isVisited);
numIslands(i, j + 1, grid, isVisited);
}
};
lintcode-433-岛屿的个数的更多相关文章
- LintCode 433. 岛屿的个数(Number of Islands)
LintCode 433. 岛屿的个数(Number of Islands) 代码: class Solution: """ @param grid: a boolean ...
- lintcode 443.岛屿的个数
在v2ex上看到有人提到了这个,感觉挺简单的,没忍住还是试一下.... 基本的染色法. AC代码: public class Solution { /** * @param grid a boolea ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- 随手练——LintCode 433 - 小岛数量
LintCode 433: https://www.lintcode.com/problem/number-of-islands/description LintCode 434: https://w ...
- [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 ...
- [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 ...
- leetcode 岛屿的个数 python
岛屿的个数 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包 ...
- 岛屿的个数12 · Number of Islands12
[抄题]: 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. [ [1, 1, 0, 0, 0], [0, 1, 0, 0 ...
- lintcode433 岛屿的个数
岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 您在真实的面试中是否遇到过这个题? Yes 样例 在矩阵: ...
- [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 ...
随机推荐
- C# 操作word 模板 值 替换
1.引用 aspose.words dll 2.word 使用doc 3.给word 模板中添加要替换位置的 书签 .引用 aspose.words dll .word 使用doc .给word ...
- 随记181120Service Fabric问题
https://github.com/Azure/service-fabric-issues/issues/1056 不能启动node one /five 问题
- 弄清Spark、Storm、MapReduce的这几点区别才能学好大数据
很多初学者在刚刚接触大数据的时候会有很多疑惑,比如对MapReduce.Storm.Spark三个计算框架的理解经常会产生混乱. 哪一个适合对大量数据进行处理?哪一个又适合对实时的流数据进行处理?又该 ...
- php安装redis拓展
1. 查看是否安装redis库 查看是否安装redis库了.可以通过下面2种方式查看. phpinfo()是否能输出redis的加载信息 在命令行执行`php -m` 输出gd 2. 安装redis库 ...
- 20155213 实验三《敏捷开发与XP实践》实验报告
20155213 实验三<敏捷开发与XP实践>实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)> ...
- 20155222 2016-2017-2 《Java程序设计》实验三
20155222 2016-2017-2 <Java程序设计>实验三 1 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单 ...
- WPF MVVM从入门到精通5:PasswordBox的绑定
原文:WPF MVVM从入门到精通5:PasswordBox的绑定 WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM从入门到精通 ...
- spring boot启动报内存溢出的问题
问题: springBoot项目,已经两次了,启动报内存溢出,内存泄露 分析: 内存泄露是因为垃圾回收器想要回收程序不用的对象,但是该对象还有引用存在 解决: 1.第一次是mybatis文件和Java ...
- 【洛谷P2252】取石子游戏
题面 题解 威佐夫博弈 代码 #include<cstdio> #include<algorithm> #include<cmath> #define RG reg ...
- 【LG3703】[SDOI2017]树点涂色
[LG3703][SDOI2017]树点涂色 题面 洛谷 题解 更博辣,更博辣!!! 猪年的第一篇博客 一次只能染根到\(x\),且染的颜色未出现过 这句话是我们解题的关键. 设\(x\)到根的颜色数 ...