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(孤岛计数)的更多相关文章

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

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

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

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

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

  6. LeetCode 305. Number of Islands II

    原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...

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

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

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

  10. (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 ...

随机推荐

  1. 性能测试Loadrunner与Mysql

    1.库文件下载地址:http://files.cnblogs.com/files/xiaoxitest/MySQL_LoadRunner_libraries.zip 分别库文件和代码添加到Loadru ...

  2. TOSCA自动化测试工具--How to modify windows

    1.页面窗口(高亮的部分是我们需要的所有窗口) 2.窗口可以任意拖拽到任何地方 3.窗口可以并列显示 任务栏点击按钮可以继续拖动 放到自己想放的地方 4.收起preview,调整宽窄 5.保存当前wi ...

  3. java并发内存模型

    java中线程之间的共享变量存储在主内存(java堆)中,每个线程都有一个私有的本地内存,本地内存存储了该线程以读.写共享变量的副本.本地内存是一个抽象概念,并不真实存储.它涵盖了cache,寄存器记 ...

  4. Selenium+Python常见定位方法

    参见官网:http://selenium-python.readthedocs.io/locating-elements.html 有多种策略来定位页面中的元素.你可以使用最适合你的情况.Seleni ...

  5. HBuilder 详细使用方法 -------------参考 :http://www.runoob.com/w3cnote/hbuilder-intro.html

    HBuilder是DCloud(数字天堂)推出的一款支持HTML5的Web开发IDE.HBuilder的编写用到了Java.C.Web和Ruby.HBuilder本身主体是由Java编写,它基于Ecl ...

  6. Python Date 1–Hello world print

    对比学习Python与C str1 = 'hello python 2'# 字符串i = 3.1415926535 print(str1)print("hello python\n" ...

  7. Java-性能调优工具-jstat

    jps 查看当前java进程 [ ~]# jps 9939 Resin 9874 WatchdogManager 18293 Jps jstat -gc -t pid 1s [ ~]# jstat - ...

  8. Statement与PreparedStatement

    Statement 用于通用查询,能批处理 PreparedStatement(简称PS) 用于执行参数化查询,能批处理 什么是参数化查询? 指在设计与数据库链接并访问数据时,在需要填入数值或数据的地 ...

  9. c# c++通信--命名管道通信

    进程间通信有很多种,windows上面比较简单的有管道通信(匿名管道及命名管道) 最近做个本机c#界面与c++服务进行通信的一个需求.简单用命名管道通信.msdn都直接有demo,详见下方参考. c+ ...

  10. 20145328 《Java程序设计》第5周学习总结

    20145328 <Java程序设计>第5周学习总结 教材学习内容总结 语法和继承架构 异常处理关键字 第八章内容主要是对Java的异常处理 Java的异常处理是通过5个关键字来实现的:t ...