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:

  1. [[0,0,1,0,0,0,0,1,0,0,0,0,0],
  2. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  3. [0,1,1,0,1,0,0,0,0,0,0,0,0],
  4. [0,1,0,0,1,1,0,0,1,0,1,0,0],
  5. [0,1,0,0,1,1,0,0,1,1,1,0,0],
  6. [0,0,0,0,0,0,0,0,0,0,1,0,0],
  7. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  8. [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:

  1. [[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

  1. class Solution {
  2. public int maxAreaOfIsland(int[][] grid) {
  3. int max_area = ;
  4. for(int i = ; i < grid.length; i++)
  5. for(int j = ; j < grid[].length; j++)
  6. if(grid[i][j] == )max_area = Math.max(max_area, AreaOfIsland(grid, i, j));
  7. return max_area;
  8. }
  9.  
  10. public int AreaOfIsland(int[][] grid, int i, int j){
  11. if( i >= && i < grid.length && j >= && j < grid[].length && grid[i][j] == ){
  12. grid[i][j] = ;
  13. return + AreaOfIsland(grid, i+, j) + AreaOfIsland(grid, i-, j) + AreaOfIsland(grid, i, j-) + AreaOfIsland(grid, i, j+);
  14. }
  15. return ;
  16. }
  17. }

Max Area of Island的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  3. [leetcode]python 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 ...

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

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

  5. 【leetcode】Max Area of Island

    国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...

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

  8. [Swift]LeetCode695. 岛屿的最大面积 | 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 ...

  9. 【easy】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) ...

随机推荐

  1. AI 强化学习

    强化学习(reinforcement learning,简称RL), agent policy state action 目标  最大化累计reward 参考链接: https://en.wikipe ...

  2. Golang 入门 : 数组

    数组是指一系列同一类型数据的集合.数组中包含的每个数据被称为数组元素(element),这种类型可以是任意的原始类型,比如 int.string 等,也可以是用户自定义的类型.一个数组包含的元素个数被 ...

  3. 一个方法教你认识ref(简单易懂)

    参数分为值类型和引用类型,当我们将一个值类型的参数进行传递到另一个方法的时候相当于,将这个变量进行复制到该方法进行操作,但是不会对该变量原始的值有影响. 但是有时候我们需要他有“影响”于是ref就出现 ...

  4. Linux下redis的安装及配置

    1.去官网下载redis(redis.io) 2.将其解压到根目录下 3.进入解压的目录,然后编译源程序, 如果不是root账户登录的,命令前面需要加sudo make make install PR ...

  5. 小程序——返回上个页面触发事件(onUnload)

    //页面销毁前--上传被提交的数据 onUnload:function(){ var _this=this; let updateStatus = wx.getStorageSync('UpdateS ...

  6. Grovvy带参数的闭包

    定义带参数的闭包:

  7. flask 状态保持session和上下文session的区别

    问题场景: 在falsk项目中导入了两个session:    首先,配置文件config.py文件中 有个 flask_session扩展导入了Session  ( from flask_sessi ...

  8. 16.kubernetes的RBAC

    role 分为clsterrole和role 我们从普通的role 开始理解起 [root@master ~]# kubectl create role pod-read --verb=get,lis ...

  9. [模板] 网络流相关/最大流ISAP/费用流zkw

    最大流/ISAP 话说ISAP是真快...(大多数情况)吊打dinic,而且还好写... 大概思路就是: 在dinic的基础上, 动态修改层数, 如果终点层数 \(>\) 点数, break. ...

  10. 【ARC101F】Robots and Exits 树状数组

    题目大意 有 \(n\) 个机器人和 \(m\) 个出口. 这 \(n\) 个机器人的初始位置是 \(a_1,a_2,\ldots,a_n\),这 \(m\) 个出口的位置是 \(b_1,b_2,\l ...