[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: 输入: ...
随机推荐
- PMP 第五章 项目范围管理
1.范围管理主要是干什么?什么是产品范围?什么是项目范围? 项目范围管理包括确保项目做而且只做成功完成项目所需的全部工作的各过程.管理项目范围主要是在定义和控制哪些工作应该包括在项目内,哪些不应 ...
- hpunix下11gRac的安装
一.检查环境 1.操作系统版本# uname -a 2.补丁包三大补丁包#swlist -l bundle|grep QPKAPPS#swlist -l bundle|grep QPKBASE#swl ...
- java thread run and start
在java中继承Thread,线程启动有两中方法:start()和run().下面简单介绍一下两者的区别. start():启动一个线程,此时线程处于就绪状态,然后调用Thread对象的run()方法 ...
- jbox使用总结
jbox是一个不错的插件 当使用get打开新页面的时候,可以使用h.对像ID来获得对像ID的值 Js代码 js代码: /** * @description: test * @author: BrinP ...
- Win7下用IIS发布网站
安装IIS控制面板->程序->程序和功能, 点击左侧的“打开或关闭Windows功能”把这几项都勾上吧,虽然有些不是必须的,多勾无碍. 进入IIS管理器控制面板-> 系统和安全-&g ...
- java net编程
转自:http://www.cnblogs.com/linzheng/archive/2011/01/23/1942328.html 一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台 ...
- 【JAVA】数字相加
设计思想:定义双精度变量 i 和 s=0 ,定义另一个变量 n 作为计数,方便之后求平均值.程序里将数据输入 i 中,通过 s 来将各个数据求和,最后除 n 得平均值. 程序流程图: 源程序代码: p ...
- poj1061 Exgcd
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...
- mongodb学习01介绍
安装/运行 查看当前mongodb运行情况: pgrep mongo; 在当前路径下建立数据库: mkdir -p data/db 按照一个数据库路径运行mongod --dbpath ./data/ ...
- express-1 从Node开始
hello world var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { ...