/** 200.Number of Islands * 2016-4-3 by Mingyang* union 什么:两个相邻的1元素* union 目的:union后计数union集合数量(通过计数union数组中根节点数量)*/class UF {public int count = 0;public int[] id = null;public UF(int m, int n, char[][] grid) {for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(grid[i][j] == '1') count++;}}id = new int[m * n];for(int i = 0; i < m * n; i++) {id[i] = i;}}public int find(int p) {while(p != id[p]) {id[p] = id[id[p]];p = id[p];}return p;}public boolean isConnected(int p, int q) {int pRoot = find(p);int qRoot = find(q);if(pRoot != qRoot) return false;else return true;}public void union(int p, int q) {int pRoot = find(p);int qRoot = find(q);if(pRoot == qRoot) return;id[pRoot] = qRoot;count--;}}public int numIslands(char[][] grid) {if(grid.length == 0 || grid[0].length == 0) return 0;int m = grid.length, n = grid[0].length;UF uf = new UF(m , n, grid);for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(grid[i][j] == '0') continue;int p = i * n + j;int q;if(i > 0 && grid[i - 1][j] == '1') {q = p - n;uf.union(p, q);}if(i < m - 1 && grid[i + 1][j] == '1') {q = p + n;uf.union(p, q);}if(j > 0 && grid[i][j - 1] == '1') {q = p - 1;uf.union(p, q);}if(j < n - 1 && grid[i][j + 1] == '1') {q = p + 1;uf.union(p, q);}}}return uf.count;}//当然你也会觉得下面的可能更简单,那就是另外一种情况了,我就是用下面的方法做的//设一个叫count的值,没遇到一个1,就把所有相连的1全部变为0,这样,到底遇到几次1,就是最终有几个小岛啦public int numIslands2(char[][] grid) {if (grid == null || grid.length == 0 || grid[0].length == 0)return 0;int count = 0;for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[0].length; j++) {if (grid[i][j] == '1') {count++;dfs(grid, i, j);}}}return count;}public void dfs(char[][] grid, int i, int j) {// validity checkingif (i < 0 || j < 0 || i > grid.length - 1 || j > grid[0].length - 1)return;// if current cell is water or visitedif (grid[i][j] != '1')return;// set visited cell to '0'grid[i][j] = '0';// merge all adjacent landdfs(grid, i - 1, j);dfs(grid, i + 1, j);dfs(grid, i, j - 1);dfs(grid, i, j + 1);}

转载于:https://www.cnblogs.com/zmyvszk/p/5569437.html

200.Number of Islands相关推荐

  1. 200. Number of Islands**(岛屿数量)

    200. Number of Islands**(岛屿数量) https://leetcode.com/problems/number-of-islands/ 题目描述 Given an m x n ...

  2. LeetCode 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 surro ...

  3. [LeetCode.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 ...

  4. leetcode 200. Number of Islands | 200. 岛屿数量(Java)

    题目 https://leetcode-cn.com/problems/number-of-islands/ 题解 class Solution {public int numIslands(char ...

  5. LeetCode 200. Number of Islands--c++ dfs解法

    LeetCode 200. Number of Islands LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++,Pyt ...

  6. 岛屿的个数java_LeetCode 200:岛屿数量 Number of Islands

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

  7. 305. Number of Islands II

    305. Number of Islands II 方法1: Union Find 易错点: Complexity 方法2: dfs Complexity A 2d grid map of m row ...

  8. 【LeetCode 剑指offer刷题】回溯法与暴力枚举法题6:Number of Islands

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Number of Islands Given a 2d grid map of '1's (land) and ' ...

  9. 【Leetcode】Number of Islands

    题目链接:https://leetcode.com/problems/number-of-islands/ 题目: Given a 2d grid map of '1's (land) and '0' ...

最新文章

  1. ios网络层优化深入浅出
  2. Spring Cloud Alibaba - 02 SpringCloud 、 SpringCloud Alibaba 、SpringBoot的生产版本选择
  3. 汇编语言 + Visual Studio 2019——Visual Studio 2019 中汇编语言环境解决方案
  4. odata协议里filter操作自带的函数 - endswith
  5. java 实现压缩单个文件
  6. DataSet与Xml之间的转换
  7. TypeError: rose() takes 0 positional arguments but 1 was given--python报错
  8. Project Euler Problem 10-Summation of primes
  9. idea 文件只读不可编辑--解决方法
  10. 吗 极域软件可以装win10_关于win10企业版在极域电子教室软件 v4.0 2015 豪华版的全屏控制下如何取得自由...
  11. SLAM中的泰勒展开
  12. ZZULIOJ 1014: 求三角形的面积
  13. PHPStorm+Xdebug配置(phpStudy)
  14. 准大四生,现在是七月中旬,要为秋招准备什么?
  15. HDCTF-2nd复盘
  16. 【mmdetection3d】——3D 目标检测 NuScenes 数据集
  17. 为什么投资拉勾的是前程无忧?
  18. 编码器的分类及工作原理
  19. 01背包.动态规划.c语言实现
  20. b360m能插HTML,没有双M.2接口怎敢自称为主流级B360

热门文章

  1. 在main()之前,IAR都做了啥?
  2. 《LeetCode力扣练习》第39题 组合总和 Java
  3. ubuntu安装python编译器_在Ubuntu上安装/编译grpc时出错
  4. mysql merge union_MySQLMerge存储引擎
  5. vivo计算机的隐藏功能介绍,vivo手机有哪些隐藏功能?这6个功能实在太好用了,要悄悄用起来...
  6. python安装了运行不了_python详细安装教程
  7. anaconda3配置环境变量_Python:Anaconda安装及LabelMe配置(1)
  8. java 移动支付接口开发,移动支付平台间接口报文解析技术核心架构实现、及平台交易处理项目全程实录教程...
  9. C++知识点30——使用C++标准库(关联容器map及其初始化,赋值,查找,添加,删除与迭代器失效)
  10. vue ts prop