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) ...
随机推荐
- 13.Linux键盘驱动 (详解)
版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一节分析输入子系统内的intput_handler软件处理部分后,接下来我们开始写input_dev驱动 本节目标: 实现键盘驱动,让开发板的 ...
- temp-重庆农商行二次出差
1, 住宿(远舰商务酒店) 与胡仕川一起住 1722房间, 178-27=151(返现后). 7月30日 7月31日 8月1日 8月2日 8月3日 2, 住宿(郎菲酒店)一个人住, 158 ...
- java面试题整理(1)
1.Equals与==的区别? ==是判断两个变量或者实例是不是指向同一个内存地址 equals是判断两个变量或者实例所指向的内存地址中的值是不是相同 2.Object有哪些公用方法? 方法equal ...
- 高德地图markers生成和点击
因为自己平时上班也是比较忙,遇到什么写什么,希望能给现在的你一些帮助,都是自己在工作中遇到的问题,给自己一个提醒,也是分享 相信很多人在做高德地图开发的时候,对于新手,官方的demo解读单个marke ...
- Eclipse 多行复制并且移动失效
Eclipse 有个好用的快捷键 即 多行复制 并且移动 但是 用 Win7 的 电脑 的 时候 发现屏幕 在 旋转 解决方案: 打开Intel的显卡控制中心 把旋转 的 快捷键 进行更改 就好 ...
- MongDB开启权限认证
在生产环境中MongoDB已经使用有一段时间了,但对于MongoDB的数据存储一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),最近在酷壳网看了一篇技术文章(https://cools ...
- 用es6的class关键字定义一个类
es6新增class关键字使用方法详解. 通过class关键字,可以定义类.基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让对象原型的写法 ...
- 【转】css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?
摘要: css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响? 一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class ...
- JavaWeb(一)Servlet中的request与response
一.HttpServletRequest概述 1.1.HttpServletRequest简介 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP ...
- 大数据开发 | MapReduce介绍
1. MapReduce 介绍 1.1MapReduce的作用 假设有一个计算文件中单词个数的需求,文件比较多也比较大,在单击运行的时候机器的内存受限,磁盘受限,运算能力受限,而一旦将单机版程序扩展 ...