Skip to content

Latest commit

 

History

History
 
 

200. Number of Islands

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

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

Related Topics:
Depth-first Search, Breadth-first Search, Union Find

Similar Questions:

Solution 1. Union Find

// OJ: https://leetcode.com/problems/number-of-islands/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class UnionFind {
    vector<int> id;
public:
    UnionFind(int N) : id(N) {
        iota(begin(id), end(id), 0);
    }
    void connect(int x, int y) {
        int a = find(x), b = find(y);
        if (a == b) return;
        id[a] = b;
    }
    int find(int x) {
        return id[x] == x ? x : (id[x] = find(id[x]));
    }
};
class Solution {
    int M, N;
    int key(int x, int y) { return x * N + y; }
    int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
public:
    int numIslands(vector<vector<char>>& A) {
        if (A.empty() || A[0].empty()) return 0;
        M = A.size(), N = A[0].size();
        UnionFind uf(M * N);
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                if (A[i][j] == '0') continue;
                for (auto &dir : dirs) {
                    int x = i + dir[0], y = j + dir[1];
                    if (x < 0 || y < 0 || x >= M || y >= N || A[x][y] == '0') continue;
                    uf.connect(key(i, j), key(x, y));
                }
            }
        }
        unordered_set<int> s;
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                if (A[i][j] == '0') continue;
                s.insert(uf.find(key(i, j)));
            }
        }
        return s.size();
    }
};

Solution 2. DFS

// OJ: https://leetcode.com/problems/number-of-islands/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class Solution {
    int M, N;
    int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    void dfs(vector<vector<char>>& A, int x, int y) {
        if (x < 0 || y < 0 || x >= M || y >= N || A[x][y] != '1') return;
        A[x][y] = 'x';
        for (auto &dir : dirs) dfs(A, x + dir[0], y + dir[1]);
    }
public:
    int numIslands(vector<vector<char>>& A) {
        if (A.empty() || A[0].empty()) return 0;
        int ans = 0;
        M = A.size(), N = A[0].size();
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                if (A[i][j] != '1') continue;
                ++ans;
                dfs(A, i, j);
            }
        }
        return ans;
    }
};

Solution 3. BFS

// OJ: https://leetcode.com/problems/number-of-islands/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class Solution {
    int M, N;
    int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    void bfs(vector<vector<char>> &A, int x, int y) {
        queue<pair<int, int>> q;
        q.emplace(x, y);
        A[x][y] = 'x';
        while (q.size()) {
            x = q.front().first, y = q.front().second;
            q.pop();
            for (auto &dir : dirs) {
                int i = x + dir[0], j = y + dir[1];
                if (i < 0 || j < 0 || i >= M || j >= N || A[i][j] != '1') continue;
                A[i][j] = 'x';
                q.emplace(i, j);
            }
        }
    }
public:
    int numIslands(vector<vector<char>>& A) {
        if (A.empty() || A[0].empty()) return 0;
        int ans = 0;
        M = A.size(), N = A[0].size();
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                if (A[i][j] != '1') continue;
                ++ans;
                bfs(A, i, j);
            }
        }
        return ans;
    }
};