zoukankan      html  css  js  c++  java
  • LeetCode 200. 岛屿数量

    习题地址 https://leetcode-cn.com/problems/number-of-islands/

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

    示例 1:

    输入:
    11110
    11010
    11000
    00000
    
    输出: 1

    示例 2:

    输入:
    11000
    11000
    00100
    00011
    
    输出: 3

    解法

    广度遍历或者使用并查集 这里使用并查集。注意将二维数组转换为一维的方法

    代码

     1 class UnionFind {
     2 public:
     3     vector<int> father;
     4     UnionFind(int num) {
     5         for (int i = 0; i < num; i++) {
     6             father.push_back(i);    //每个人都指向自己
     7         }
     8     }
     9     int Find(int n) {
    10         //非递归版本 
    11         /*
    12         while (father[n] != n) {
    13             n = father[n];
    14         }
    15         return n;
    16         */
    17         //递归
    18         if (father[n] == n)
    19             return n;
    20         father[n] = Find(father[n]);
    21         return father[n] ;
    22     }
    23     void Union(int a, int b) {
    24         int fa = Find(a);
    25         int fb = Find(b);
    26         father[fb] = fa;
    27     }
    28 };
    29 
    30 
    31 class Solution {
    32 public:
    33     int directions[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    34     int encode(int i,int j,int n){
    35         return i*n+j;
    36     }
    37     int numIslands(vector<vector<char>>& grid) {
    38         int M = grid.size();
    39         if(M == 0)
    40             return 0;
    41         int N = grid[0].size();
    42         if(N == 0)
    43             return 0;
    44         UnionFind UF(M*N);
    45         for(int i = 0;i < M;i++){
    46             for(int j =0;j < N;j++){
    47                 if(grid[i][j] == '1'){
    48                     for(int d =0;d < 4; d++){
    49                         int di = directions[d][0];
    50                         int dj = directions[d][1];
    51                         if(i+di >= 0 && i+di < M && j+dj >=0&& j+dj < N &&
    52                             grid[i+di][j+dj] == '1')
    53                         {
    54                             UF.Union(encode(i,j,N),encode(i+di,j+dj,N));
    55                         }
    56                     }
    57                 }
    58             }
    59         }
    60         
    61         int res = 0;
    62         for(int i =0;i < M;i++){
    63             for(int j = 0; j< N;j++){
    64                 if(grid[i][j] == '1'){
    65                     int id = encode(i,j,N);
    66                     if(UF.Find(id) == id)
    67                         res++;
    68                 }
    69             }
    70         }
    71         
    72         return res;
    73         
    74     }
    75 };
    View Code
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    494. Target Sum 添加标点符号求和
    636. Exclusive Time of Functions 进程的执行时间
    714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票
    377. Combination Sum IV 返回符合目标和的组数
    325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
    275. H-Index II 递增排序后的论文引用量
    274. H-Index论文引用量
    RabbitMQ学习之HelloWorld(1)
    java之struts2的数据处理
    java之struts2的action的创建方式
  • 原文地址:https://www.cnblogs.com/itdef/p/10920023.html
Copyright © 2011-2022 走看看