zoukankan      html  css  js  c++  java
  • 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 water.

    Example 1:

    11110
    11010
    11000
    00000

    Answer: 1

    Example 2:

    11000
    11000
    00100
    00011

    Answer: 3

    Similar: 
    • 130. Surrounded Regions
    • 305. Number of Islands II
    • 323. Number of Connected Components in an Undirected Graph

    Solution 1. DFS

     1 public class Solution {
     2     public int numIslands(char[][] grid) {
     3         if (grid.length == 0 || grid[0].length == 0)
     4             return 0;
     5             
     6         int num = 0;
     7         for (int i = 0; i < grid.length; i++) {
     8             for (int j = 0; j < grid[0].length; j++) {
     9                 if (grid[i][j] == '1') {
    10                     num++;
    11                     dfs(grid, i, j);
    12                 }
    13             }
    14         }
    15         return num;
    16     }
    17     public void dfs(char[][] grid, int i, int j) {
    18         grid[i][j] = '0';
    19         if (i > 0 && grid[i - 1][j] == '1') {
    20             dfs(grid, i - 1, j);
    21         }
    22         if (j > 0 && grid[i][j - 1] == '1') {
    23             dfs(grid, i, j - 1);
    24         }
    25         if (i < grid.length - 1 && grid[i + 1][j] == '1') {
    26             dfs(grid, i + 1, j);
    27         }
    28         if (j < grid[0].length - 1 && grid[i][j + 1] == '1') {
    29             dfs(grid, i, j + 1);
    30         }
    31     }
    32 }

     Solution 2. Union Find

     1 /* Solution 2. Union Find
     2  * Tip: convert 2d grid to 1d
     3  */
     4 public class Solution {
     5     public int numIslands(char[][] grid) {
     6         if (grid.length == 0 || grid[0].length == 0)
     7             return 0;
     8             
     9         int m = grid.length;
    10         int n = grid[0].length;
    11         int cnt = 0;
    12         
    13         // construct points and edges
    14         int[] pt = new int[m*n];
    15         ArrayList<int[]> edges = new ArrayList<>();
    16         
    17         for (int i=0; i<m; i++) {
    18             for (int j=0; j<n; j++) {
    19                 int id = i * n + j;
    20                 pt[id] = id;
    21                 if (grid[i][j] == '1') {
    22                     cnt++;
    23                     if (i+1<m && grid[i+1][j] == '1') {
    24                         edges.add(new int[] {id, id+n}); // down connect
    25                     }
    26                     if (j+1<n && grid[i][j+1] == '1') {
    27                         edges.add(new int[] {id, id+1}); // right connect
    28                     }
    29                 }
    30             }
    31         }
    32         
    33         for (int[] edge : edges) {
    34             int root1 = find(pt, edge[0]);
    35             int root2 = find(pt, edge[1]);
    36             
    37             if (root1 != root2) {
    38                 pt[root2] = root1;
    39                 cnt--;
    40             }
    41         }
    42         return cnt;
    43     }
    44     
    45     private int find(int[] pt, int id) {
    46         while (pt[id] != id) {
    47             pt[id] = pt[pt[id]];
    48             id = pt[id];
    49         }
    50         return id;
    51     }
    52 }
  • 相关阅读:
    VS2017专业版和企业版激活密钥
    jquery 中的回调函数,回调函数(callback)是什么?
    artDialog
    MVC 5 视图之公用代码
    联想服务器thinkserver rd650安装 windows server 2008 r2
    c#类库中使用Session
    C#导出Excel按照指定格式设置单元格属性值
    visual studio 2017使用NHibernate4.0连接oracle11g数据库
    NHibernate连接oracle报错
    第五章 面向方面编程___通知类型
  • 原文地址:https://www.cnblogs.com/joycelee/p/5279075.html
Copyright © 2011-2022 走看看