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 }
  • 相关阅读:
    并发编程与高并发学习笔记六
    并发编程与高并发学习笔记五
    并发编程与高并发学习笔记四
    浅谈数据挖掘与机器学习
    IntelliJ Idea常用的快捷键
    单链表的插入,查找,删除
    二叉树的序列化和反序列化及完全二叉树和满二叉树的区别
    二叉树的按层遍历并实现换行
    冒泡排序,选择排序,插入排序,快速排序的比较及优化
    axSpA患者新发炎症更容易发生在既往发生过炎症的区域
  • 原文地址:https://www.cnblogs.com/joycelee/p/5279075.html
Copyright © 2011-2022 走看看