leetcode: Number of Islands(200)

原创 2016年08月31日 15:19:11

https://leetcode.com/problems/number-of-islands/


class Solution {
public:
    bool dfs(int i,int j,vector<vector<char>> &grid)
    {
       if(i>=0&&i<grid.size()&&j>=0&&j<grid[i].size()&&grid[i][j]=='1')
        {
            grid[i][j] = '3';
            dfs(i-1,j,grid);
            dfs(i+1,j,grid);
            dfs(i,j-1,grid);
            dfs(i,j+1,grid);
            return true;
        }
        return false;
    }
    int numIslands(vector<vector<char>> &grid) {
        int num = 0;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[i].size();j++)
            {
                if(dfs(i,j,grid))
                    num++;
            }
        }
        return num;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】

【200-Number of Islands(岛的数目)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao...

LeetCode 200. Number of Islands (并查集)

LeetCode 200. Number of Islands (并查集)LeetCode 200 Number of Islands 并查集 问题描述 解题思路 参考代码Tags: * Depth...

第三周:( LeetCode200 ) Number of Islands(c++)

原题:本题是经典的简单图论问题,求岛的个数也即求图上的连通区间的个数。可采用深搜或者广搜。假设共有n个点,时间复杂度为o(n)。本题笔者采用深搜的递归和非递归两种解法。将遍历过的“1”被改为“0”,其...

LeetCode 200. Number of Islands

DFS遍历即可。

leetcode解题之200. Number of Islands Java版(岛屿的数量)

leetcode解题之200. Number of Islands Java版(岛屿的数量) ,深度优先遍历

<LeetCode OJ> 200 / 130 Number of Islands / Surrounded Regions

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium Given a 2d grid map of '1's ...

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

leetcode Add to List 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 surr...

[LeetCode]200. Number of Islands

[LeetCode]200. Number of Islands题目描述思路BFS深搜,遍历,如果点为1,计数+1,并用深搜的方式将和他连通的为1的点均置0GFS留坑代码BFSclass Soluti...
  • Lcharon
  • Lcharon
  • 2017年03月07日 17:17
  • 3028

LeetCode题解–200. Number of Islands

链接LeetCode题目:https://leetcode.com/problems/number-of-islands/难度:Medium题目 Given a 2d grid map of ‘1...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode: Number of Islands(200)
举报原因:
原因补充:

(最多只允许输入30个字)