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) 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的更多相关文章
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)
Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...
- 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 用了第一种方式, ...
- [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 ...
- C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...
- 【leetcode】Max Area of Island
国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...
- 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 ...
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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 ...
随机推荐
- 每天CSS学习之line-height
line-height是CSS的一个属性,其作用是设置行高.其有以下几种值: 1.normal:自动设置合理的行间距.该值是默认值.如下示例: p{ line-height:normal; } 结果: ...
- vue-router-8-路由组件传参
在组件中使用$route会使之与其对应路由形成高度耦合,使用props解耦 const User = { props: ['id'], template: '<div>User{{ id ...
- Java 整体测试重点题 错题积累
重点题 错题积累 1: 解析: %d:用来设置输出日志的日期和时间 %m:用来输出代码中指定的消息 %n:用来输出一个回车换行符 %l:用来输出日志事件的发生位置 %p:用来输出优先级 %f:用 ...
- TCP/IP协议 计算机间的通讯,传输、socket 传输通道
#! /usr/bin/env python3 # -*- coding:utf-8 -*- #TCP/IP简介 #为了把全世界的所有不同类型的计算机都连接起来,就必须规定一套全球通用的协议,为了实现 ...
- SQL-1 选取表中某一属性最大值的所有信息 查找最晚入职员工的所有信息
题目描述 查找最晚入职员工的所有信息CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT NULL,`fi ...
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程(转)
reference:https://www.cnblogs.com/tracyone/p/3561781.html 按照ISE后,建立ISE启动连接: 这个帖子作为我安装xilinx ISE 14.7 ...
- 百度地图API示例:鼠标绘制点线面 控件修改
需求 :在使用地图API时,绘制工具栏控件想自己选择哪些要,哪些不要. 可以查看相应的类:官网地址: http://api.map.baidu.com/library/DrawingManager/1 ...
- pytest的执行规则和顺序
用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效(不在 ...
- VOOKI:一款免费的Web应用漏洞扫描工具
Vooki是一款免费且用户界面友好的Web应用漏扫工具,它可以轻松地为你扫描任何Web应用并查找漏洞.Vooki主要包括三个部分,Web应用扫描器,Rest API扫描器以及报告.Web应用扫描器V ...
- php优秀框架codeigniter学习系列——前言
php的框架众多,笔者用过的包括thinkphp,CI,smarty,laravel,也用过一些公司自己开发的框架. thinkphp是国人自己开发的,我大概用过一段时间,基本功能都还好,应该也还比较 ...