200. Number of Islands

problem description

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:

Input:
11110
11010
11000
00000

Output: 1

Example 2:

Input:
11000
11000
00100
00011

Output: 3

algorithm thought

这题不是很难,直接遍历所有,对每个为1的点,进行DFS遍历。没遍历到一个1的节点就赋值为0. 每次进行DFS的时候,将res+1.

code

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int res=0;
        for(int i=0;i<grid.size();++i){
            for(int j=0;j<grid[0].size();++j){
                if(grid[i][j]=='1'){
                    DFS(grid,i,j);
                    res++;
                }
            }
        }
        return res;
    }
    void DFS(vector<vector<char>> &grid, int x, int y)
    {
        grid[x][y] = '0';
        if(x > 0 && grid[x - 1][y] == '1')
            DFS(grid, x - 1, y);
        if(x < grid.size() - 1 && grid[x + 1][y] == '1')
            DFS(grid, x + 1, y);
        if(y > 0 && grid[x][y - 1] == '1')
            DFS(grid, x, y - 1);
        if(y < grid[0].size() - 1 && grid[x][y + 1] == '1')
            DFS(grid, x, y + 1);
    }
};

algorithm analysis

对于二维数组,每个节点最多访问两次,所以总的最多访问的次数是2*n².最后时间复杂度O(n²)

Last updated