Number of Islands -- leetcode

原创 2015年07月07日 19:27:13

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



基本思路:

1.遍历二维数组每个元素

2. 如果发现其为 '1', 则发现了一块岛屿,岛屿数量+1, 并从此点进行深度优先遍历,将该岛屿所有元素置上访问标记。

   递归遍历,即是向上、下、左、右,都进行访问。直到边界或者遇到水('0')、已访问('2)'为止。

3. 最后将访问标记恢复为'1'。此步为可选,不恢复,leetcode也不会报错。


class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int ans = 0;
        if (grid.empty() || grid[0].empty())
            return ans;
        
        const int m = grid.size();
        const int n = grid[0].size();
        for (int i=0; i<m; i++) {
            for (int j=0; j<n; j++) {
                if (grid[i][j] == '1') {
                    ++ans;
                    dfs(grid, i, j);
                }
            }
        }
        
        for (int i=0; i<m; i++) {
            for (int j=0; j<n; j++)
                if (grid[i][j] == '2')
                    grid[i][j] = '1';
        }
        return ans;
    }
    
    void dfs(vector<vector<char> >&grid, int i, int j) {
        if (j < 0 || i < 0 || 
            i == grid.size() || 
            j == grid[0].size() ||
            grid[i][j] != '1')
            return;
        
        grid[i][j] = '2';
        dfs(grid, i+1, j);
        dfs(grid, i-1, j);
        dfs(grid, i, j+1);
        dfs(grid, i, j-1);
    }
};



[LeetCode]200. Number of Islands

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

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

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

LeetCode Number of Islands

Description: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands...

[leetcode] 305. Number of Islands II 解题报告

题目链接: https://leetcode.com/problems/number-of-islands-ii/ A 2d grid map of m rows and n columns i...

LeetCode题解–200. Number of Islands

链接LeetCode题目:https://leetcode.com/problems/number-of-islands/难度:Medium题目 Given a 2d grid map of ‘1...

Leetcode-200. Number of Islands

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2016年12月30日 21:11
  • 135

leetcode_Number of Islands_medium--dfs

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is sur...

(LeetCode 200)Number of Islands(并查集、DFS)

Q: Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is s...

[LeetCode305]Number of Islands II

Hard..A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLan...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Number of Islands -- leetcode
举报原因:
原因补充:

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