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 ...
随机推荐
- 基于Redis+MySQL+MongoDB存储架构应用
摘 要: Redis+MySQL+MongoDB技术架构实现了本项目中大数据存储和实时云计算的需求.使用MongoDB切片的水平动态添加,可在不中断平台业务系统的同时保障扩容后的查询速度和云计算效能 ...
- DFS----Lake Counting (poj 2386)
Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...
- svn服务器搭建及使用(三)
接下来,试试用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态, ...
- TreeMap - 源代码学习笔记
TreeMap 实现了 NavigableMap 接口,而NavigableMap 接口继承于 SortedMap接口. 所有本文还会记录 SortedMap 和 NavigableMap 的阅读笔记 ...
- Python发送四种格式的post请求的代码样例
x-www-form-urlencoded: path = "/api/v1/topics/update"params={'accesstoken':'d38a77f0-6f29- ...
- fortran学习网站
https://www.sciencesoft.cn/Fortran/Index.htm http://m.fcode.cn/
- netty源码理解(二) serverstrap.bind()
eventloop是一个线程,里面有一个executor封装了一个线程工厂,在启动的时候启动一个线程,传入的实现了runnable的内部类,里面调用了eventloop的run方法.
- Python 嵌套
1 要yuanhao的首字母大写 li = [1,2,5,'taibai','yuanhao',[1,'alex',3,],True]li[4] = 'Yuanhao'print(li) li[4] ...
- chromium ②
这篇研究两个问题:chromium对线程的封装和进程通信.主要参考chromium的官方技术文档:Treading和Inter-process Communication (IPC). chrome速 ...
- 客户端优化之使用javascript原生方法替代复杂的数学运算和jquery方法
尽管jQuery等js框架相比原生javascript使用起来极为方便但是为什么在一些大型互联网公司还是一致强调前端开发人员的js基础,因为尽管javascript使用起来可能非常不便不仅体现在语法而 ...