首页 > 其他 > 详细

200. Number of Islands

时间:2018-11-06 10:37:25      阅读:123      评论:0      收藏:0      [点我收藏+]
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




If not allowed to change the input :   写一下
Can change 1 to 2 after visited , and change 2 back to 1 after done 
Or use a Boolean array to check if visited 


// Dfs  mine 
   
class Solution {
    public int numIslands(char[][] grid) {
      int count = 0;
      for(int i = 0; i < grid.length; i++){
        for(int j = 0; j < grid[0].length; j++){
          if(grid[i][j] == ‘1‘){
            dfs(i, j, grid);
            count++;
          }
        }
      }
      return count;
    }
  
    private void dfs(int row, int col, char[][] grid){
      
      if(row < 0 || col < 0 || row >= grid.length || col >= grid[0].length || grid[row][col] == ‘0‘){
        return;
      }else {
        grid[row][col] = ‘0‘;
        dfs(row + 1, col, grid);
        dfs(row - 1, col, grid);
        dfs(row, col - 1, grid);
        dfs(row, col + 1, grid);
      }
    }
}



// Union find , not mine 


class UF {

public int count = 0;
public int[] id = null;

public UF(int m, int n, char[][] grid) {
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            if(grid[i][j] == ‘1‘) count++;
        }
    }
    id = new int[m * n];
    for(int i = 0; i < m * n; i++) {
        id[i] = i;
    }
}

public int find(int p) {
    while(p != id[p]) {
        id[p] = id[id[p]];
        p = id[p];
    }
    return p;
}

public boolean isConnected(int p, int q) {
    int pRoot = find(p);
    int qRoot = find(q);
    if(pRoot != qRoot) return false;
    else return true;
}

public void union(int p, int q) {
    int pRoot = find(p);
    int qRoot = find(q);
    if(pRoot == qRoot) return;
    id[pRoot] = qRoot;
    count--;
}
}

public int numIslands(char[][] grid) {
    if(grid.length == 0 || grid[0].length == 0) return 0;
    int m = grid.length, n = grid[0].length;
    UF uf = new UF(m , n, grid);
    
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            if(grid[i][j] == ‘0‘) continue;
            int p = i * n + j;
            int q;
            if(i > 0 && grid[i - 1][j] == ‘1‘) {  //  up
                q = p - n;
                uf.union(p, q);
            }
            if(i < m - 1 && grid[i + 1][j] == ‘1‘) {  // down
                q = p + n;
                uf.union(p, q);
            }
            if(j > 0 && grid[i][j - 1] == ‘1‘) {  // left
                q = p - 1;
                uf.union(p, q);
            }
            if(j < n - 1 && grid[i][j + 1] == ‘1‘) {    // right
                q = p + 1;
                uf.union(p, q);
            }
        }
    }
    return uf.count;
}



// uf my version   TLE ??
class Solution {
    public int numIslands(char[][] grid) {
      int m = grid.length;
      int n = grid[0].length;
      UnionFind uf = new UnionFind(m* n);
      int count = 0;
      int[][] directions = {{0, 1}, {0,-1}, {-1, 0}, {1, 0}};
      for(int i = 0; i < grid.length; i++){
        for(int j = 0; j < grid[0].length; j++){
          if(grid[i][j] == ‘1‘){
            count++;
            for (int[] dir : directions){
              int x = i + dir[0];
              int y = j + dir[1];
              
              if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == ‘0‘){
                continue;
              }else{
                if(uf.union(getKey(i, j, n), getKey(x, y, n))){
                  count--;
                }
              }
            }
            
          }
        }
      }
      return count;
      
    }
    public int getKey(int i, int j, int m){
      return i * m + j;
    }
  
    class UnionFind{
      int[] id;
      
      public UnionFind(int n){
        this.id = new int[n];
        for(int i = 0; i < n; i++){
          id[i] = i;
        }
      }
      
      
      public int find (int n){
        int root = n;
        while(n != id[n]){
          n = id[n];
        }
        
        while(n != root){
          int next = id[n];
          id[n] = root;
          n = next;
        }
        return root;
      }
      
      public boolean union( int q, int p){
        int rootp = find(p);
        int rootq = find(q);
        if(rootp == rootq){
          return false;
        }else{
          id[rootp] = rootq;
          return true;
        }
      }
    }
}

 

200. Number of Islands

原文:https://www.cnblogs.com/tobeabetterpig/p/9913330.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!