zoukankan      html  css  js  c++  java
  • Leetcode: 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:
    
    11110
    11010
    11000
    00000
    Answer: 1
    
    Example 2:
    
    11000
    11000
    00100
    00011
    Answer: 3

    DFS的Flood Fill方法,

    不使用额外visited数组,但是用‘1’变成‘2’表示visited的方法

    复杂度

    时间 O(NM) 空间 O(max(N,M)) 递归栈空间

     1 public class Solution {
     2     public int numIslands(char[][] grid) {
     3         if (grid==null || grid.length==0 || grid[0].length==0) return 0;
     4         int count = 0;
     5         for (int i=0; i<grid.length; i++) {
     6             for (int j=0; j<grid[0].length; j++) {
     7                 if (grid[i][j] != '1') continue;
     8                 else {
     9                     count++;
    10                     floodFill(grid, i, j);
    11                 }
    12             }
    13         }
    14         return count;
    15     }
    16     
    17     public void floodFill(char[][] grid, int i, int j) {
    18         if (i<0 || i>=grid.length || j<0 || j>=grid[0].length) return;
    19         if (grid[i][j] != '1') return; //either 0(water) or 2(visited)
    20         grid[i][j] = '2';
    21         floodFill(grid, i-1, j);
    22         floodFill(grid, i+1, j);
    23         floodFill(grid, i, j-1);
    24         floodFill(grid, i, j+1);
    25     }
    26 }

    Union Find: (Quick Union)

     1 class Solution {
     2     public int numIslands(char[][] grid) {
     3         if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
     4         int m = grid.length;
     5         int n = grid[0].length;
     6         unionFind uf = new unionFind(m * n);
     7         for (int i = 0; i < m; i ++) {
     8             for (int j = 0; j < n; j ++) {
     9                 if (grid[i][j] == '0') continue;
    10                 uf.fathers[i * n + j] = i * n + j;
    11                 uf.count ++;
    12                 if (i > 0 && grid[i - 1][j] == '1') uf.union(i * n + j, (i - 1) * n + j);
    13                 if (j > 0 && grid[i][j - 1] == '1') uf.union(i * n + j, i * n + j - 1);
    14             }
    15         }
    16         return uf.count;
    17     }
    18     
    19     class unionFind {
    20         int[] fathers;
    21         int count;
    22         public unionFind(int n) {
    23             this.fathers = new int[n];
    24             Arrays.fill(fathers, -1);
    25             this.count = 0;
    26         }
    27         
    28         public void union(int i, int j) {
    29             if (isConnected(i, j)) return; 
    30             int iRoot = find(i);
    31             int jRoot = find(j);
    32             fathers[iRoot] = jRoot;
    33             count --;
    34         }
    35         
    36         public int find(int i) {
    37             while (fathers[i] != i) {
    38                 i = fathers[i];
    39             }
    40             return i;
    41         }
    42         
    43         public boolean isConnected(int i, int j) {
    44             return find(i) == find(j);
    45         }
    46     }
    47 }

    follow up是找最大岛的面积,想法是设置两个全局变量 maxArea, area, area负责记录每个岛的面积,上面代码第8行每次遇到‘1’就置为1,然后在recursion里面++,用area的最大值更新maxArea

    follow up是数unique island, 比如

    110000

    110001

    001101

    101100

    100000

    总共两个unique岛,不是四个

    方法可以是记录每次新的岛屿搜索的路径,left,right,up,down, 作为标志是否相同的key,存hashset

    code 参 FB面经 Prepare: Count Unique Islands

  • 相关阅读:
    node.js+mysql接口入门
    input边写边验证?正则表达式写在属性里?小技巧
    创建vue,react项目
    jquery在网页中加载本地json文件
    OpenFeigin服务接口调用
    Ribbon负载均衡服务调用
    Consul服务注册与发现
    Eureka服务注册与发现
    springboot项目在idea实现热部署
    设计模式——单例模式
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4416153.html
Copyright © 2011-2022 走看看