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.

 class Solution {
public int maxAreaOfIsland(int[][] grid) {
int max_area = ;
for(int i = ; i < grid.length; i++)
for(int j = ; j < grid[].length; j++)
if(grid[i][j] == )max_area = Math.max(max_area, AreaOfIsland(grid, i, j));
return max_area;
} public int AreaOfIsland(int[][] grid, int i, int j){
if( i >= && i < grid.length && j >= && j < grid[].length && grid[i][j] == ){
grid[i][j] = ;
return + AreaOfIsland(grid, i+, j) + AreaOfIsland(grid, i-, j) + AreaOfIsland(grid, i, j-) + AreaOfIsland(grid, i, j+);
}
return ;
}
}

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. Windows 支持 OpenSSH 了!

    从 Win10 1809 和 Windows Server 2019 开始 Windows 开始支持 OpenSSH Server.本文介绍一下其基本的概念和配置方法,本文演示用的环境为 Win10 ...

  2. python之路6-迭代器、生成器、装饰器

    1.迭代器&生成器 列表生成式 现在有个需求,列表[1,2,3,4,5,6,7,,8,9],要求把列表里的每个值加1,如何实现? 方法一: list = [1,2,3,4,5,6,7,8,9] ...

  3. 常用CSS3

    (ಥ_ಥ)    啊啊,我的胃好疼啊.感觉最近胃又开始不舒服了.啊——果然老了呢,想当初,我也是不坏金刚之身来着呢,唉,我的可怜的小胃胃   (ಥ_ಥ) 记录几个已经常见的不能再常见的css3样式. ...

  4. Converting PDF to Text in C#

    Parsing PDF files in .NET using PDFBox and IKVM.NET (managed code). Download source files - 82 kB [c ...

  5. cglib 代理实现

    cglib代理的原理是通过继承实现对目标对象的代理 //1.接口 package cn.itcast.service; public interface UserService { void save ...

  6. luogu5316

    P5316 恋恋的数学题 题目背景 恋恋是个可爱的女孩子,最近她沉迷了做数学题. 题目描述 现在恋恋正在处理的题目十分简单:现在有k \space (2\leq k\leq 4)k (2≤k≤4)个数 ...

  7. 清北学堂part1

    睡眠质量相当高的一天(滑稽) 整一整都学了啥 1:高精度(相当水,毕竟学过) 2:模运算(?! 这还要讲?) 3:快速幂(还要谢一位学习高数时间为我们讲解的同学...不得不说真的有效,快速幂已经是随手 ...

  8. python中self和cls的区别

    1.self表示一个具体的实例本身.如果用了staticmethod,那么就可以无视这个self,将这个方法当成一个普通的函数使用. 2.cls表示这个类本身. >>> class ...

  9. Eclipse maven hadoop -- java.io.IOException: No FileSystem for scheme: hdfs

    2019-01-10 概述 今天在Windows系统下新安装了Eclipse和maven的环境,想利用Maven构建一个Hadoop程序的,结果却发现程序运行时一直报 “No FileSystem f ...

  10. 动态库 Framework

    framework的建立和生成 都比较简单.重点会放在第三块上面(指令集说明及合并) 1.framework target建立 1.1. command + shift + N 选取 ios -> ...