Max Area of Island

最新更新请见:https://yanjia.me/zh/2019/02/...

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.

深度优先搜索

思路

该题基本上就是Number of Islands的变形题,唯一的区别是在Number of Islands中我们只需要将搜索到的陆地置为0,保证其不会再被下次探索所用就行了。但这题多了一要求就是要同时返回岛屿的面积。那么最简单的方式就是在递归的时候,每个搜索到的格子都将自身的面积1,加上四个方向搜索出来的延伸面积都加上,再返回给调用递归的那个格子作为延伸面积使用,这样一直返回到岛屿的起始格子时,面积之和就是岛屿的总面积了。

代码

Go

func maxAreaOfIsland(grid [][]int) int {maxArea := 0for i := range grid {for j := range grid[i] {area := measureIsland(grid, i, j)if area > maxArea {maxArea = area}}}return maxArea
}func measureIsland(grid [][]int, x, y int) int {if grid[x][y] == 0 {return 0}area := 1grid[x][y] = 0if x > 0 {area += measureIsland(grid, x-1, y)}if x < len(grid)-1 {area += measureIsland(grid, x+1, y)}if y > 0 {area += measureIsland(grid, x, y-1)}if y < len(grid[0])-1 {area += measureIsland(grid, x, y+1)}return area
}

[Leetcode] Max Area of Island 最大岛屿面积相关推荐

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

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

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

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

  3. LeetCode 695. Max Area of Island

    LeetCode 695. Max Area of Island Given a non-empty 2D array grid of 0's and 1's, an island is a grou ...

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

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

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

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

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

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

  8. 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], ...

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

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

最新文章

  1. 进程的用户栈和内核栈
  2. 如何编写简单的lua小脚本@@
  3. 产品更新教程-钉钉宜搭版本:手写签名和定位组件来了
  4. linux-shell命令之chown(change owner)【更改拥有者】
  5. Windows 64 位 mysql 5.7以上版本包解压中没有data目录和my-default.ini以及服务无法启动的解决办法以及修改初始密码的方法...
  6. Activity Monitor 闪退 无法进入睡眠
  7. 计算机相关技能简历,简历计算机技能有哪些
  8. itext-asian-5.2.0
  9. lintcode刷题——左叶子的和
  10. Verilog算法入门
  11. HDMI音视频传输协议
  12. CTF学习之0基础入门笔记(一)
  13. 为什么说买腾讯云服务器通过代理商购买更划算
  14. 【GZH逸佳君】生财有术:100多位互联网赚钱高手分享的2021年5万字赚钱建议
  15. 上海人设提示访问接口出错
  16. 计算机病毒教学评课,计算机病毒评课稿.doc
  17. 基于php食堂外卖系统
  18. 京东和苏宁的大战,我还是喜欢京东
  19. 【数据结构】CH6 数组和广义表
  20. console调linux设备,linux设备驱动之console控制台驱动

热门文章

  1. 和AI结对编程!OpenAI与GitHub联手推出AI代码生成工具,比GPT-3更强大
  2. SAP QM 含有Long-term检验特性的检验批UD完成后还能继续录入检验结果?
  3. 百度造车和RoboTaxi利好自动驾驶?不,利好茅台
  4. SAP HUM 嵌套HU初探 II
  5. 年终收藏!吴恩达盘点2020年度AI热门事件
  6. 什么是生成模型和GAN?一文体验计算机视觉的魔力
  7. 计算机视觉成安防“显学”落地仍需解决这些难题
  8. SAP MM 明明有维护源清单,还是不能下PO?
  9. 2019年中国重点大学排名发布,北大第1,清华第2,浙大第5
  10. 算力不是王道,强化学习之父Rich Sutton的六点错误