c++ solution (dfs)


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.