说明

算法:Number of Islands
LeetCode地址:https://leetcode.com/problems/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:

Input:
11110
11010
11000
00000Output: 1

Example 2:

Input:
11000
11000
00100
00011Output: 3

解题思路

问题类型:图的深度优先搜索算法。
求岛屿的个数,实际为上下左右连通的图的个数。题目给出了数目条件为’1’, 实际还有一个隐含条件,就是连在一起的’1’,表示已经被访问过。
标志为新岛屿的充分必要条件就变成了 grid[row][column] == '1' && visitArray[row][column] == false

时间复杂度为 O(rowcolumn) , 标志是否已经访问过得子方法 markConnectIsland ,时间复杂度为O(rowcolumn), 也就是均摊为每个数据都访问一次,也就是跟父方法 numIslands 时间复杂度一样。

代码实现

import java.util.Arrays;public class NumberOfIslands {public int numIslands(char[][] grid) {if (grid == null || grid.length == 0 || grid[0].length == 0) {return 0;}int count = 0;int rowLen = grid.length;int columnLen = grid[0].length;boolean[][] visitArray = new boolean[rowLen][columnLen];for (int row = 0; row < rowLen; row++) {for (int column = 0; column < columnLen; column++) {if (grid[row][column] == '1' && visitArray[row][column] == false) {markConnectIsland(grid, visitArray, row, column);count++;}}}return count;}private void markConnectIsland(char[][] grid, boolean[][] visitArray, int row, int column) {if (row < 0 || row >= grid.length) return;if (column < 0 || column >= grid[0].length) return;if (grid[row][column] != '1' || visitArray[row][column]) return;visitArray[row][column] = true;markConnectIsland(grid, visitArray, row + 1, column);markConnectIsland(grid, visitArray, row - 1, column);markConnectIsland(grid, visitArray, row, column + 1);markConnectIsland(grid, visitArray, row, column - 1);}public static void main(String[] args) {//char[][] grid = {//                    {'1', '1', '1', '1', '0'},//                    {'1', '1', '0', '1', '0'},//                    {'1', '1', '0', '0', '0'},//                    {'0', '0', '0', '0', '0'}//                };char[][] grid = {{'1', '1', '0', '0', '0'},{'1', '1', '0', '0', '0'},{'0', '0', '1', '0', '0'},{'0', '0', '0', '1', '1'}};int count = new NumberOfIslands().numIslands(grid);System.out.println("input: " + Arrays.toString(grid) + "\nOutput:" + count );}
}

运行结果

input: [[C@610455d6, [C@511d50c0, [C@60e53b93, [C@5e2de80c]
Output:3

代码执行效率

Runtime: 5 ms, faster than 37.70% of Java online submissions for Number of Islands.
Memory Usage: 39.9 MB, less than 78.00% of Java online submissions for Number of Islands.

总结

图的深度优先搜索。

代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/popular/NumberOfIslands.java

算法:Number of Islands(岛屿的个数)相关推荐

  1. lintcode:Number of Islands 岛屿的个数

    题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [[1, 1, 0, 0, 0],[ ...

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

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

  3. 【算法python实现】 -- 岛屿的个数

    原题: https://leetcode-cn.com/problems/number-of-islands/ 思路 深度优先遍历 从顶点开始,到其相邻的一个节点,再由此节点至其相邻的节点,依次遍历所 ...

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

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

  5. LintCode 433. 岛屿的个数 JavaScript算法

    描述 给一个 01 矩阵,求不同的岛屿的个数. 0 代表海,1 代表岛,如果两个 1 相邻,那么这两个 1 属于同一个岛.我们只考虑上下左右为相邻. 样例 - 样例 1:输入: [[1,1,0,0,0 ...

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

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

  7. 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 ...

  8. 【Leetcode】Number of Islands

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

  9. 灰狼算法优化LSTM超参数-神经元个数-dropout-batch_size

    1.摘要 本文主要讲解:使用灰狼算法优化LSTM超参数-神经元个数-dropout-batch_size 主要思路: 灰狼算法 Parameters : 迭代次数.狼的寻值范围.狼的数量 LSTM P ...

  10. 305. Number of Islands II

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

最新文章

  1. 深度学习与计算机视觉系列(4)_最优化与随机梯度下降\数据预处理,正则化与损失函数
  2. mysql分析表命令_MySql分析整理命令
  3. 实现strstr库函数功能
  4. 最详细的Spring核心IOC的源码分析
  5. 使用event.keyCode来判断是否为数字
  6. linux----LAMP之编译安装apache
  7. 25 The Go image/draw package go图片/描绘包:图片/描绘包的基本原理
  8. C语言学习心得 Caiwentao
  9. 一帆风顺幼儿园管理软件 v3.01 bt
  10. SODB、RBSP和EBSP
  11. 正则验证手机号码和邮箱格式
  12. 计算机键盘数字和确键失控,电脑键盘数字键失灵怎么办
  13. 小米路由器 mini 重新刷回官方固件
  14. IDEA统计代码量Statistic插件
  15. 代码审查工具FxCop建议采用的规则总结
  16. 量子计算机为什么低温,突破量子计算机瓶颈!超低温芯片能在接近绝对零度的温度下工作...
  17. python爬取boss直聘招聘信息_Python 爬取boss直聘招聘信息!
  18. C++三目运算符(简述)
  19. GD32F103学习笔记(8)——ADC接口使用
  20. foj2198 Problem 2198 快来快来数一数 dp 矩阵快速幂

热门文章

  1. java底层 文件操作,java底层是怎样对文件操作的
  2. python web工资怎么样_月薪21170的Python Web岗,学到什么程度能找到工作?
  3. java关闭流方法,Java关闭流方法总结
  4. 解决SpringBoot集成Redis出现RedisConnectionException: Unable to connect to 192.168.64.100:6379
  5. 瑞典驻华参赞:智慧城市建设提升为国家战略
  6. LCD驱动程序详细讲解(三)
  7. 如何在Kubernetes中暴露服务访问 1
  8. shell编程基础(二)
  9. find命令使用及实例
  10. 【RS】BGP14条选路原则(1)