LeetCode OJ:Number of Islands(孤岛计数)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
计算孤岛的数量,注意上下左右为0就是孤岛,孤岛可以很大,grid范围以外的都算是water。代码如下所示:
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int count = ;
if(!grid.size() || !grid[].size())
return count;
int row = grid.size();
int col = grid[].size();
for(int i = ; i < row; ++i){
for(int j = ; j < col; ++j){
if(grid[i][j] == ''){
dfs(grid, i, j);
count++;
}
}
}
return count;
}
void dfs(vector<vector<char>> & grid, int x, int y)
{
if(grid[x][y] == '')
grid[x][y] = 'X';
else
return;
dfs(grid, x+, y);
dfs(grid, x-, y);
dfs(grid, x, y+);
dfs(grid, x, y0);
}
};
LeetCode OJ:Number of Islands(孤岛计数)的更多相关文章
- [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 ...
- [LeetCode] 305. Number of Islands II 岛屿的数量 II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- Java for 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 ...
- [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 ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- 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 ...
- 【leetcode】Number of Islands(middle)
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 ...
- (BFS/DFS) 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 ...
随机推荐
- windows上使用clang编译程序
环境:windows7,64位 1.下载并安装llvm,安装包里除了llvm,也有clang: http://releases.llvm.org/5.0.0/LLVM-5.0.0-win64.exe ...
- linux 用到的命令
1. cd / 直接到服务器根目录 2. pwd 查看 当前所在目录 3. ll 查看当前文件下文件列表 4.ls 查看当前文件下文件,横向排列 5.ps -ef | gre ...
- SiteMesh使用(2.4.2)
SiteMesh是一个网页布局和修饰的框架.我理解的是在一个母版页上引入页面各个位置的信息,从而拼接成一个页面展示出来.它定义了一个过滤器,把页面统一加上头部和底部. 我的项目是在springmvc中 ...
- yum安装memchache
转载地址:http://www.cnblogs.com/jiunadianshi/articles/2001334.html 标准的CentOS5软件仓库里面是没有memcache相应的包的,所以,我 ...
- linux 安装vscode
滚动安装vscode 需要先添加源,然后install 以centos为例: sudo rpm --import https://packages.microsoft.com/keys/microso ...
- ERROR 1093 (HY000): You can't specify target table 'test' for update in FROM clause
MySQL执行更新语句报错: 更新语句:UPDATE test SET state=50 WHERE num IN(SELECT num FROM test WHERE state=60): 报错:E ...
- (转)国内yum源的安装(163,阿里云,epel)
国内yum源的安装(163,阿里云,epel) ----阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- 20145322《Java程序设计》第5次实验报告
20145322<Java程序设计>第5次实验报告 实验内容 1.根据所学内容,编写代码实现服务器与客户端 2.掌握密码技术的使用 3.设计安全传输系统,客户端中输入明文,利用DES算法加 ...
- Java基础东西(按位操作运算)
http://aokunsang.iteye.com/blog/615658 前奏: 昨天一哥们问我Java位移你会吗,我说不会,想想位移这么麻烦,一般有位移的Java代码一律不看,有几个人会啊, ...
- [P1860]新魔法药水
题目描述 商店里有N种药水,每种药水都有一个售价和回收价.小S攒了V元钱,还会M种魔法,可以把一些药水合成另一种药水.他一天可以使用K次魔法,问他一天最多赚多少钱? 输入输出格式 输入格式: 第一行四 ...