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. 用random模块实现验证码

    #! /usr/bin/env python3 import random checkcode = "" ## 全部为数字的验证码 # for i in range(4): # c ...

  2. Python3.x:pytesseract识别率提高(样本训练)

    Python3.x:pytesseract识别率提高(样本训练) 1,下载并安装3.05版本的tesseract 地址:https://sourceforge.net/projects/tessera ...

  3. JavaScript&jQuery获取url参数方法

    JavaScript&jQuery获取url参数方法 function getUrlParam(name){ var reg = new RegExp("(^|&)" ...

  4. 非root权限 安装更新gcc

    本文主要参考网络上文章,并根据自己安装出现的问题进行补充. 参考文章: 1.gcc和boost的升级步骤(非root权限):https://blog.csdn.net/u010246947/artic ...

  5. # 20145327 《Java程序设计》第七周学习总结

    20145327 <Java程序设计>第七周学习总结 教材学习内容总结 只有Lambda表达式,参数的类型必须写出来,如果有目标类型,在编译程序可推断出类型的情况下,可以不写出. GMT时 ...

  6. Linux下C连接MySql数据库

    目录: 一.解决小的问题: 二.大问题,如果你不小心把/usr/lib的所属用户改了导致sudo命令用不了: 三.C连接MySql编程本身: 其实写这个程序真的很简单,十多分钟的事情,只是以前没在Li ...

  7. BootStrap实现图片轮播

    <div class="container">        <div data-ride="carousel" id="carou ...

  8. 4.9版本的linux内核中温度传感器adt7461的驱动源码在哪里

    答:drivers/hwmon/lm90.c,这个文件中支持了好多芯片,内核配置项为CONFIG_SENSORS_LM90 Location: -> Device Drivers -> H ...

  9. LeetCode——Unique Binary Search Trees II

    Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...

  10. LA 3971 组装电脑(二分)

    https://vjudge.net/problem/UVALive-3971 题意:你有b块钱,想要组装一台电脑.给出n个配件各自的种类.品质因子和价格,要求每种类型的配件各买一个,总价格不超过b, ...