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 }
  • 相关阅读:
    SpringBoot-配置Druid-yml方式
    CentOS7下配置Nginx并实现简单的负载均衡
    用私有构造器或者枚举类型强化Singleton
    virtualenv虚拟环境的使用
    Windows平台安装Python
    window平台基于influxdb + grafana + jmeter 搭建性能测试实时监控平台
    easy-mock本地部署成功,访问报错:EADDRNOTAVAIL 0.0.0.0:7300 解决方案
    npm install 报错: WARN checkPermissions Missing write access to 解决方案
    npm install 报错:ERR! code EINTEGRITY 解决方案
    最新版chrome浏览器如何离线安装crx插件?(转载)
  • 原文地址:https://www.cnblogs.com/joycelee/p/5279075.html
Copyright © 2011-2022 走看看