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) 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.
题目标签:Array
题目给了我们一个 2d grid array, 让我们找到所有岛中区域最大的一个,返回区域值。0代表海洋,1代表陆地。陆地与陆地相连,只能是横向和纵向,不可以斜着。
因为只能横向和纵向相连,所以每一个cell 只能是4个方向延伸,左 上 右 下。
这道题目要用到Depth-first Search,遍历2d array,遇到1的时候,就利用dfs把这个岛的区域大小找全。我的dps顺序是 左,上,右,下。在递归dfs之前,要把目前的cell
设为0,是为了避免dfs又往回走,每一个数过的cell,就不需要在重复走了。
题外话:最近因为看不了极限挑战,所以这几天看了东方卫视的另一个节目 <梦想改造家4> , 挺好看的,特别是第4集。各位程序猿休息的时候可以看看!谁都不是一座孤岛!加油刷题!
Java Solution:
Runtime beats 53.35%
完成日期:10/22/2017
关键词:Array
关键点:DFS
- class Solution
- {
- public int maxAreaOfIsland(int[][] grid)
- {
- int max_area = 0;
- for(int i=0; i<grid.length; i++)
- {
- for(int j=0; j<grid[0].length; j++)
- {
- if(grid[i][j] == 1)
- max_area = Math.max(max_area, dfs(grid, i, j));
- }
- }
- return max_area;
- }
- public int dfs(int[][] grid, int i, int j)
- {
- // if i or j is invalid or grid is 0, just return 0
- if( i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0)
- return 0;
- // do dfs to its 4 direction cell when value is 1
- int tempMaxArea = 1;
- grid[i][j] = 0; // set current cell to 0 to prevent dfs coming back
- // order is left, top, right, bottom
- tempMaxArea += dfs(grid, i, j-1) + dfs(grid, i-1, j) + dfs(grid, i, j+1) + dfs(grid, i+1, j);
- return tempMaxArea;
- }
- }
参考资料:
https://discuss.leetcode.com/topic/106301/java-c-straightforward-dfs-solution
LeetCode 题目列表 - LeetCode Questions List
LeetCode 695. Max Area of Island (岛的最大区域)的更多相关文章
- leetcode 695 Max Area of Island 岛的最大面积
这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...
- [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 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 ...
- 【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] 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 ...
- 200. Number of Islands + 695. Max Area of Island
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【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) ...
随机推荐
- MySql各种查询
1.简单查询 select * from info; select Code as '代号',name as '姓名' from info; 2.条件查询 select * from car wher ...
- JVM(三)内存回收(一)
最近花了相当长一段时间在看Hotspot JVM的GC和内存分配,本节先总结和回顾一下内存回收的相关知识点,内存的分配放到下节再讨论. 一.什么是JVM的GC GC即Garbage Collectio ...
- this的四种绑定形式
一 , this的默认绑定 当一个函数没有明确的调用对象的时候,也就是单纯作为独立函数调用的时候,将对函数的this使用默认绑定:绑定到全局的window对象. 一个例子 function fire ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- JAVA HashMap 解析
1.简介(其实是HashMap注释的大致翻译) 本文基于JDK1.8,与JDK1.7中的HashMap有一些区别,看官注意区别. HashMap实现了Map接口,提供了高效的Key-Value访问.H ...
- java关于随机数和方法重构
1.生成随机数 源代码 package Zuote; public class SuiJiShu { public static void main( String args[] ) { java.u ...
- 在前端页面对easyui中的datagrid与jqgrid加载后的数据进行操作
因为项目的需求,需要在grid中加载数据后再在前端页面执行操作,所以在easyui中的grid与jqgrid都进行了测试和操作: eayui中grid数据的操作: //构造集合对象 var list ...
- 邮件实现详解(二)------手工体验smtp和pop3协议
上篇博客我们简单介绍了电子邮件的发送和接收过程,对参与其中的邮件服务器,邮件客户端软件,邮件传输协议也有简单的介绍.我们知道电子邮件需要在邮件客户端和邮件服务器之间,以及两个邮件服务器之间进行传递必须 ...
- codesmith连接Mysql提示“找不到请求的 .Net Framework Data Provider。可能没有安装。"
1,首先需要将MySql.Data.dll复制到codesmith安装目录下bin文件夹下,注意dll的版本 2,其次因为codesmith7采用的是.net4.0的配置文件,(64位系统)找到C:\ ...
- WPF 中模拟键盘和鼠标操作
转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...