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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
 
分析: 对每次出现'1'的区域进行计数,同时深度或广度遍历,然后置为'x'。
class Solution
{
private:
void dfs(vector<vector<char> > &grid, int x, int y)
{
if(x < || x >= grid.size()) return;
if(y < || y >= grid[].size()) return;
if(grid[x][y] != '') return; grid[x][y] = 'x';
dfs(grid, x-, y);
dfs(grid, x+, y);
dfs(grid, x, y-);
dfs(grid, x, y+);
}
public:
int numIslands(vector<vector<char> > &grid)
{
if(grid.empty() || grid[].empty()) return ;
int X = grid.size(), Y = grid[].size();
int count = ; for(int i=; i<X; i++)
{
for(int j=; j<Y; j++)
{
if(grid[i][j] == '')
{
count++;
dfs(grid, i, j);
}
}
} return count;
}
};

[leetcode] Number of Islands的更多相关文章

  1. [LeetCode] 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 ...

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

  3. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  4. [LeetCode] Number of Islands II

    Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...

  5. LeetCode – 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 ...

  6. LeetCode – Number of Islands

    Given a -d grid map of 's (water), count the number of islands. An island is surrounded by water and ...

  7. LeetCode Number of Islands 岛的数量(DFS,BFS)

    题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...

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

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

随机推荐

  1. Legolas工业自动化平台入门(一)搭建应用

    前两篇给大家介绍了TWaver家族的新面孔--Legolas工业自动化平台,通过两个应用案例钻井平台工程用车和水源地监控系统,相信大家对Legolas已经有了一定程度的了解.这几篇文章,我们会逐步介绍 ...

  2. 自己动手搭建 CAS(Central Authentication Service) 环境,为了单点登录(Single Sign On , 简称 SSO )

    介绍 刚刚搭建 CAS 成功了,现在记录下来,怕以后忘记,同时也给需要帮助的人.CAS 搭建需要服务端和客户端,服务端是 Java 写的,如果自己搭建则需要安装与配置 Java 环境.客户端可以是多种 ...

  3. 跨域名 Cookie 传递测试

    问题 我打开了 www.aaa.com 里面的一个页面(www.aaa.com/hello.php),其中这个 hello.php 里面包含一个 <img> 标签,里面的 src 来自于 ...

  4. Hadoop入门进阶课程13--Chukwa介绍与安装部署

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan  ...

  5. gulp-uglify《JS压缩》----gulp系列(四)

    本节实现JS压缩,在实现压缩前,先配置JS任务,设置源目录和输出目录. 在系列(三)代码的基础上,再进行扩展. 1.找到gulp->config.js,对JS进行源目录(src->img) ...

  6. 30个实用的 Photoshop 动作《免费下载》

    这篇文章向设计师们分享30个实用的 Photoshop 动作下载.Photoshop 的动作是用集合了许多步的操作的集合,使执行任务自动化,这为设计者在进行图像处理的操作上带来很多方便.同时用户还可以 ...

  7. .NET C# 使用S22.Imap.dll接收邮件 并且指定收取的文件夹的未读邮件,并且更改未读准态

    string host = Conf.ConfigInfo.POP_Host; int port = Conf.ConfigInfo.POP_Port; string username =Conf.C ...

  8. css中filter:alpha透明度使用

    css中filter:alpha透明度使用    使用filter可以设置透明度,filter:alpha在IE下是没有问题的,要支持firefox就需要使用-moz-opacity,下面有个不错的示 ...

  9. 黑客入门之IP地址及常用命令

    在网络上,只要利用IP地址就可以找到目标主机,因此,如果黑客想要攻击某个网络主机,就要先确定该目标主机的域名或IP地址. IP地址概述 所谓IP地址就是一种主机编址方式,给每个连接在Internet上 ...

  10. 【基础】Html跨域跳转问题整理

    今天遇到一个问题,是有关 跨域跳转问题,涉及到知识比较基础. 具体问题是:  A站点的 PageA (Post数据)到 B站点的 PageB,PageB接受到后Redirect到B站的 PageC:  ...