[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.
思路:深度优先搜索。从第一行第一个开始遍历整个数组,如果某个位置的值是 1 ,那么就开始深度优先搜索。
在深度优先搜索中,我们先要看看与该位置连通有几种可能的情况,很明显是4种,即上下左右。所以我们得用
个循环来遍历这四种可能的情况。如果上或下或左或右的值是1,我们就用递归函数递归上或下或左或右的位置,
求解他们连通的情况。
class Solution {
private int sum = 0,maxSum = 0;
public int maxAreaOfIsland(int[][] grid) {
for (int i=0;i<grid.length;i++){
for (int j=0;j<grid[i].length;j++){
if (grid[i][j]==1){
sum = 0;
dfs(grid,i,j);
}
}
}
return maxSum;
}
private void dfs(int[][] grid,int i,int j){
sum +=grid[i][j];
grid[i][j]= 0;
if (i-1>=0&&grid[i-1][j]==1)
dfs(grid,i-1,j);
if (i+1<grid.length&&grid[i+1][j]==1)
dfs(grid,i+1,j);
if (j-1>=0&&grid[i][j-1]==1)
dfs(grid,i,j-1);
if (j+1<grid[i].length&&grid[i][j+1]==1)
dfs(grid,i,j+1);
if (sum>maxSum){
maxSum = sum;
} }
}
[Leetcode]695. Max Area of Island的更多相关文章
- 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 岛的最大面积
这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...
- 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】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) ...
- 695. Max Area of Island最大岛屿面积
[抄题]: 求最多的联通的1的数量 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (repre ...
随机推荐
- Centos给文件设置了777权限仍不能访问解决方案
Centos给文件设置了777权限仍不能访问: 开启了SELinux导致 1.查看SELinux状态:/usr/sbin/sestatus -v ##如果SELinux status参数为enable ...
- System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库(http://go.micro ...
- C#封装的websocket协议类
关于VB版之前已经写了,有需要可以进传送门<VB封装的WebSocket模块,拿来即用>,两个使用都差不多,这里简单概述一下: 连接完成后,没有握手就用Handshake()先完成握手之后 ...
- Autograd:自动微分
Autograd 1.深度学习的算法本质上是通过反向传播求导数,Pytorch的Autograd模块实现了此功能:在Tensor上的所有操作,Autograd都能为他们自动提供微分,避免手动计算导数的 ...
- python学习:字典
字典 1.查询内存地址 a = 10 print(id(a)) b = a print(id(b)) b = 15 print(id(b)) 2. 数据类型 不可变类型:整型.字符串.元组 可变类型: ...
- SPA 单页面应用程序。
看到这个问题,先说下自己的理解到的程度,再去参考做修正,争取这一次弄懂搞清楚 自己的理解: 单页面应用程序,解决浏览器获取数据刷新页面的尴尬,通过ajax请求获取数据达到异步更新视图的按钮,原理的实现 ...
- Android Gradle Task
Tasks runnable from root project ------------------------------------------------------------ Androi ...
- Katalon Studio之接口测试中token处理
前言 最近抽时间接触了一下Katalon Studio(后面简称KS),并且利用KS做了一些接口测试的试验,感觉还不错,不过其中接口授权中缺少通过token动态验证的方案,虽然KS支持Authoriz ...
- python语法_内置函数
a = filter(函数名,序列) 返回一个迭代器对象/.函数里必须加过滤条件 ret = ['a','b','c','d','e'] def ft(s): if s != 'a': return ...
- Vue 学习笔记 — css属性计算的问题
简书 今天在使用Vue时遇到一个问题:在切换css内联属性时某些特殊属性的计算会有问题,无法得到预期的结果. 例子: https://jsfiddle.net/blqw/cLwau40z/ 上面的页面 ...