LeetCode 695. Max Area of Island

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:
[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.
Note: The length of each dimension in the given grid does not exceed 50.
分析:求最大连通域。图的深度优先遍历算法。
代码参考:

https://www.cnblogs.com/lance-/p/3789241.html

   public int maxAreaOfIsland(int[][] grid) {int maxArea=0;//int currentArea=0;for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[0].length ; j++) {if(grid[i][j] == 1){maxArea=Math.max(eraArea(grid,i,j,1),maxArea);}}}return maxArea;}public int eraArea(int[][] grid,int i,int j,int currentArea){/*访问过就设置为0*/grid[i][j]=0;if((i-1) >=0 && grid[i-1][j] == 1){currentArea++;currentArea=eraArea(grid,i-1,j,currentArea);}if((j-1) >=0&& grid[i][j-1] == 1){currentArea++;currentArea=eraArea(grid,i,j-1,currentArea);}if((i+1) < grid.length && grid[i+1][j] == 1){currentArea++;currentArea=eraArea(grid,i+1,j,currentArea);}if((j+1)<grid[0].length && grid[i][j+1] == 1){currentArea++;currentArea=eraArea(grid,i,j+1,currentArea);}return  currentArea;}

LeetCode 695. Max Area of Island相关推荐

  1. [swift] LeetCode 695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  2. LeetCode 695 Max Area of Island

    题目: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) ...

  3. leetcode 695. Max Area of Island | 695. 岛屿的最大面积(DFS)

    题目 https://leetcode.com/problems/max-area-of-island/ 题解 class Solution {int M, N;public int maxAreaO ...

  4. LeetCode 695. Max Area of Island javascript解决方案

    题意: 寻找最大岛. leetcode.com/problems/ma- 传入: [[0,0,1,0,0,0,0,1,0,0,0,0,0],  [0,0,0,0,0,0,0,1,1,1,0,0,0], ...

  5. LeetCode - 695. Max Area of Island (Java)

    R.C记录矩阵行列 可以将邻接矩阵转为邻接表来做,即要将二维数组转换为一维数组: 将二维坐标转化为一维坐标: V = x * C + y 若将一维坐标转化为二维坐标: x = V / C y = V ...

  6. 695. Max Area of Island (Medium)——岛屿的最大面积

    前言: 本题目为深度优先遍历(DFS) 算法的一道典型例题. 题目 : 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成 ...

  7. [Leetcode] Max Area of Island 最大岛屿面积

    Max Area of Island 最新更新请见:https://yanjia.me/zh/2019/02/... Given a non-empty 2D array grid of 0's an ...

  8. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...

  9. Leetcode695.Max Area of Island岛屿的最大面积

    给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二维数组中 ...

最新文章

  1. 解决eclipse+MAVEN提示One or more constraints have not been satisfied.的问题
  2. win2012 R2的ntp时间同步设置解析
  3. 【Python】对self和_init_的理解
  4. 阿里妈妈技术团队5篇论文入选 SIGIR 2022!
  5. Linux利器:QEMU!用它模拟开发板能替代真开发板?
  6. caffe :error MSB4062: 未能从程序集** 加载任务“NuGetPackageOverlay”
  7. android win10 style,不开玩笑,安卓可以体验 Windows 10 了
  8. 网络安全应急响应-常用工具
  9. 进程间通信方式有哪些?
  10. Verilog学习之路(11)—事件控制(时间延迟)
  11. MySQL8.0学习笔记(8)—— stored procedures,function
  12. 容齐的身世_白发容齐和容乐是什么关系?容齐和容乐是兄妹吗?
  13. 两级交流放大电路实验报告
  14. 20201010基础标签用途说明
  15. 计算机图形学 | 基于MFC和二维变换的画图软件
  16. 如何固化zynq程序
  17. iOS Jenkins自动化打包 上传fir、蒲公英、邮件、钉钉提醒
  18. 微信公众平台PHP开发(转载)
  19. 光伏逆变器“核心器件”IGBT在光伏逆变器的应用
  20. Google Chrome 主题下载站 分享

热门文章

  1. Oracle表空间(Tablespace)
  2. (FSRCNN)_Accelerating the Super-Resolution Convolutional Neural Network——超分辨率(三)
  3. 近百本精品图书全部免费赠送——仅面向学生
  4. 【Java】UML类图详解-详细易懂
  5. 同时删除多个 Txt 文本文档的最后几行
  6. 【科技百咖】五舟科技CEO谢高辉:国产自主 软硬一体,通过定制化杀出一条血路...
  7. 高中计算机课程事例,高中信息技术课堂教学案例与分析.doc
  8. 基于AD Event日志检测LSASS凭证窃取攻击
  9. idea 弹出Server's certificate is not trusted 解决方法
  10. Windows语音朗读