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

思路

可以参考:Find the number of islands
有一点不同的是本题目只用考虑四个方向,而参考题目是考虑了8个方向。

代码

/*------------------------------------------------------
*   日期:2014-04-10
*   作者:SJF0115
*   题目: 200.Number of Islands
*   网址:https://leetcode.com/problems/number-of-islands/
*   结果:AC
*   来源:LeetCode
*   博客:
--------------------------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int numIslands(vector<vector<char>> &grid) {
        // 行数
        int row = grid.size();
        if(row == 0){
            return 0;
        }//if
        // 列数
        int col = grid[0].size();
        if(col == 0){
            return 0;
        }//if
        int count = 0;
        for(int i = 0;i < row;++i){
            for(int j = 0;j < col;++j){
                // 如果是1且没有被访问过则发现一个新的岛屿
                if(grid[i][j] == '1'){
                    // 以该岛屿做深度遍历
                    DFS(grid,row,col,i,j);
                    ++count;
                }//if
            }//for
        }//for
        return count;
    }
private:
    // grid 地图 row 行数 col 列数 (x,y)当前坐标
    void DFS(vector<vector<char> > &grid,int row,int col,int x,int y){
        if(x < 0 || y < 0 || x >= row || y >= col || grid[x][y] == '0'){
            return;
        }//if
        grid[x][y] = '0';
        // right
        DFS(grid,row,col,x,y+1);
        // left
        DFS(grid,row,col,x,y-1);
        // top
        DFS(grid,row,col,x-1,y);
        // bottom
        DFS(grid,row,col,x+1,y);
    }
};

int main() {
    Solution solution;
    vector<vector<char> > grid =
    {
        {'1', '1', '0', '0', '0'},
        {'1', '1', '0', '0', '0'},
        {'0', '0', '1', '0', '0'},
        {'0', '0', '0', '1', '1'}
    };
    cout<<solution.numIslands(grid)<<endl;
    return 0;
}

运行时间

这里写图片描述

目录
相关文章
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
233 1
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
195 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
124 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
270 0
LeetCode 414. Third Maximum Number
|
算法
LeetCode 405. Convert a Number to Hexadecimal
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
235 0
LeetCode 405. Convert a Number to Hexadecimal
|
API
LeetCode 375. Guess Number Higher or Lower II
我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字。 每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了。 然而,当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。直到你猜到我选的数字,你才算赢得了这个游戏。
166 0
LeetCode 375. Guess Number Higher or Lower II
|
API
LeetCode 374. Guess Number Higher or Lower
我们正在玩一个猜数字游戏。 游戏规则如下: 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。 每次你猜错了,我会告诉你这个数字是大了还是小了。
271 0
LeetCode 374. Guess Number Higher or Lower
|
算法
LeetCode 321. Create Maximum Number
给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。 求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。
151 0
LeetCode 321. Create Maximum Number
|
存储
LeetCode 313. Super Ugly Number
编写一段程序来查找第 n 个超级丑数。 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。
179 0
LeetCode 313. Super Ugly Number

热门文章

最新文章