[email protected] [200] Number of Islands

https://leetcode.com/problems/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

class Solution {
public:
    void dfs(vector<vector<char> >& grid, vector<vector<bool> >& vis, int x, int y) {
        int dir[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};

        for(int i=0;i<4;++i) {
            int nx = x + dir[i][0], ny = y + dir[i][1];
            if(nx<0 || ny<0 || nx>=grid.size() || ny>=grid[0].size() || vis[nx][ny] || grid[nx][ny] == ‘0‘) continue;
            vis[nx][ny] = true;
            dfs(grid, vis, nx, ny);
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        if(grid.size() == 0) return 0;

        vector<vector<bool> > vis(grid.size(), vector<bool>(grid[0].size(), false));

        int res = 0;
        for(int i=0;i<grid.size();++i) {
            for(int j=0;j<grid[i].size();++j) {
                if(!vis[i][j] && grid[i][j] == ‘1‘) {
                    ++res;
                    dfs(grid, vis, i, j);
                }
            }
        }

        return res;
    }
};
时间: 2024-10-28 22:51:53

[email protected] [200] Number of Islands的相关文章

200. Number of Islands

https://leetcode.com/problems/number-of-islands/#/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. Yo

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

[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

【LeetCode】200. Number of Islands (2 solutions)

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

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

&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(DFS)

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

200. Number of Islands java solutions

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