[LeetCode] Number of Islands

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:

11110110101100000000

Answer: 1

Example 2:

11000110000010000011

Answer: 3

解题思路:

以前我做过这个题目,现在竟然全忘了,折腾了好久,看来每天都要保持锻炼啊。其实这道题思路挺简单的啊,就是递归标记而已。比如,若当前元素为‘1’,就递归将其左右上下为1的元素全部标记为‘0’,计数加1。(之前做的是积水,还包括对角处)。下面是代码;

class Solution {
public:
    int numIslands(vector<vector<char>> &grid) {
        int m = grid.size();
        if(m==0){
            return 0;
        }
        int n = grid[0].size();
        if(n==0){
            return 0;
        }

        int result = 0;
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(grid[i][j] == '1'){
                    result++;
                    markRead(grid, m, n, i, j);
                }
            }
        }
        return result;
    }

    void markRead(vector<vector<char>> &grid, int m, int n, int i, int j){
        if(i<0||j<0 || i>=m||j>=n||grid[i][j]=='0'){
            return;
        }
        grid[i][j]='0';
        markRead(grid, m, n, i+1, j);
        markRead(grid, m, n, i, j+1);
        markRead(grid, m, n, i-1, j);
        markRead(grid, m, n, i, j-1);
    }
};

时间: 2024-12-21 04:14:46

[LeetCode] Number of Islands的相关文章

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand o

[LeetCode] Number of Islands II

Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands

第九篇 LeetCode Number of Islands II

UnifonFind with path compression and weighting O(klogmn), k is the length of positions array public class Solution { public List<Integer> numIslands2(int m, int n, int[][] positions) { List<Integer> result = new ArrayList<Integer>(); if

第八篇 LeetCode Number of Islands

好久没更新了,终于考完了继续回来刷题 岛屿问题属于最基本的DFS, BFS题目 使用DFS时会遇到如果图太大call stack过深的follow up,此时可以转为使用BFS 下一篇Number of Islands II 中将使用另一种Union Find的做法 public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } i

LeetCode Number of Islands 岛的数量(DFS,BFS)

题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. 1 class Solution { 2 public: 3 bool isok(vector<vector<char> >& grid,int x,int y) 4 { 5 return x>=0 && y>=0 && x<grid.siz

Number of Islands -- leetcode

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

[LeetCode][JavaScript]Number of Islands

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

&lt;LeetCode OJ&gt; 200. Number of Islands

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium 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 verticall

[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