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.

BFS + 涂色法:

class Solution {
public int maxAreaOfIsland(int[][] grid) {
if(grid == null){
return 0;
}
int row = grid.length;
int column = grid[0].length;
int max = 0;
for(int i = 0; i<row; i++){
for(int j = 0; j<column; j++){
if(grid[i][j] == 1){
//count++;
//grid[i][j] = 2;
Queue<int[]> queue = new LinkedList<>();
int count = 0;
queue.add(new int[]{i,j});
grid[i][j] = 2;
while(!queue.isEmpty()){
int[] temp = queue.poll();
int r = temp[0];
int c = temp[1];
count++;
//move up
if(r - 1 >= 0 && grid[r - 1][c] == 1){
queue.add(new int[]{r-1, c});
grid[r-1][c] = 2;
}
//move down
if(r + 1 < row && grid[r + 1][c] == 1){
queue.add(new int[]{r+1, c});
grid[r+1][c] = 2;
}
//move left
if(c - 1 >= 0 && grid[r][c - 1] == 1){
queue.add(new int[]{r, c-1});
grid[r][c-1] = 2;
}
//move right
if(c + 1 < column && grid[r][c + 1] == 1){
queue.add(new int[]{r, c+1});
grid[r][c+1] = 2;
}
}
if(count>max){
max = count;
}
} }
}
return max;
}
}

LeetCode - Max Area of Island的更多相关文章

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

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

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

  3. 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 用了第一种方式, ...

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

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

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

  6. 【leetcode】Max Area of Island

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

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

  8. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

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

随机推荐

  1. zookeeper:springboot+dubbo配置zk集群并测试

    1.springboot配置zk集群 1.1:非主从配置方法 dubbo: registry: protocol: zookeeper address: ,, check: false 1.2:主从配 ...

  2. 【阅读笔记】《C程序员 从校园到职场》第四章 变量和函数

    参考: Contents: 一.数据类型(对基本数据类型进行重定义——规范化) 二.变量和函数  (命名规则,注意事项) 三.静态变量及其使用 一.数据类型(对基本数据类型进行重定义——规范化) 1. ...

  3. [Linux]Linux下修改snmp协议的默认161端口

    一.Linux SNMP的配置 SNMP的简介和Linux下IPV4,IPV6地址的snmp协议开启可以参考上一个随笔:[Linux]CentOS6.9开启snmp支持IPV4和IPV6 二.修改默认 ...

  4. leetcode python 004

    ##  已知l1,l2均为升序数组,##  在两数组l1,l2中寻找第n位数,##  两数组中位数中,前者大于后者,说明后者中位数以下的成员必定在真正中位数之下##  可以将其剔除,剔除a个元素后的两 ...

  5. Android : 基于alsa库的音乐播放

    继上篇:Android : alsa-lib 移植 ,这篇随笔实现一个demo基于移植好的alsa库在Android平台上播放wav文件: 一.利用ffmeg将一个mp3文件转换成wav文件: (1) ...

  6. C Runtime Library、C  Runtime

    C Runtime Library.C Runtime   1)运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些 ...

  7. navicat premium 连接出现的问题

    1.listener does not currently know of service requested in connect descriptor 2.问题截图: 3.问题原因:服务名或者SI ...

  8. idea打包 - 可执行jar包

    需求:有一个基于SpringBoot的socket服务端程序,实现了对消息的接收.发送并行操作.此时想要将其构建成可执行的Jar包,执行 java -jar xx.jar后能够进行消息的收发. 分析: ...

  9. kbmMW 5.08.01压力测试报告

    上图为客户端测试结果,运行14小时,无异常报告.基于洞主封装的HttpsysTransport,基于ClientQuery完成25万多次数据库访问操作,含查询并对查询结果进行修改及增加新记录,然后提交 ...

  10. mysql随机查询记录的高效率方法

    mysql使用rand随机查询记录的高效率方法 一直以为mysql随机查询几条数据,就用 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了. 但是真 ...