leetcode 695 Max Area of Island 岛的最大面积

这个题使用深度优先搜索就可以直接遍历
DFS递归方法:
class Solution {
public:
vector<vector<int>> dirs={{-,},{,-},{,},{,}};
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res=;
int m=grid.size(),n=grid[].size();
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(grid[i][j]==) continue;
int cnt=;
dfs(grid,cnt,i,j);
res=res>cnt?res:cnt;
}
}
return res;
}
void dfs(vector<vector<int>> &grid,int &cnt,int i,int j){
int m=grid.size(),n=grid[].size();
if(i< || i>=m ||j< ||j>=n || grid[i][j]!=) return;
cnt++;
grid[i][j]=-;
for(auto dir:dirs){
dfs(grid,cnt,i+dir[],j+dir[]);
}
}
};
BFS迭代方法:使用queue
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] 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
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】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) ...
随机推荐
- TMS320F28335——下载程序到flash中
一.让CCS软件支持Flash烧写 添加F28335.cmd文件 如图屏蔽掉25335_RAM_lnk.cmd 2.支持从Flash中拷贝文件到RAM中 添加DSP2832x_MemCopy.c 在主 ...
- 简单的物流项目实战,WPF的MVVM设计模式(三)
往Services文件里面添加接口以及实现接口 IUserService接口 List<User> GetAllUser(); GetUserService类 ConnectToDatab ...
- raft协议-分布式环境下的数据一致性问题
阅读了一个有意思的ppt,是Standford大学发表的raft协议 网址:http://thesecretlivesofdata.com/raft/ 下面自己总结下咯: 1.raft是一个实现了解决 ...
- Linux学习--第二天--分区、格式化、系统安装、vmware、远程管理工具
分区 主分区加上扩展分区只能有四个,其中扩展分区只能有一个,扩展分区不能写入数据,不能格式化,只能包含逻辑分区.这是硬盘的限制. 格式化 分为高级与低级.文件系统是高级格式化.低级是硬盘操作. 扩展分 ...
- 025-Cinder服务-->安装并配置一个本地存储节点(ISCSI)
一:Cinder提供块级别的存储服务,块存储提供一个基础设施为了管理卷,以及和OpenStack计算服务交互,为实例提供卷.此服务也会激活管理卷的快照和卷类型的功能,块存储服务通常包含下列组件:cin ...
- 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- Comet OJ - Contest #2 C题 言论的阴影里妄想初萌
题目描述 Takuru 是一名能力者,他在地震时获得了念力致动的能力.所以他经常用自己的能力去干一些奇奇怪怪的事情. 有一天他获得了一张 nn 个点的无向完全图,之后他使用了能力,导致这张图的 \fr ...
- [每日一学]apache camel|BDD方式开发apache camel|Groovy|Spock
开发apache camel应用,最好的方式就是tdd,因为camel的每个组件都是相互独立并可测试的. 现在有很多好的测试框架,用groovy的Spock框架的BDD(行为测试驱动)是比较优秀和好用 ...
- 转 Nacos集群环境搭建
转载 送上nacos-server-1.1.3 链接:https://pan.baidu.com/s/11r3OeffHN8AwKLurmmzJmg 密码:wdu2 下载↓↓↓↓ https://g ...
- TLS漏洞:超过50万个电子邮件服务器容易受黑客攻击,太可怕了
2019年在流行的开源Exim电子邮件服务器软件中发现了一个关键的远程执行代码漏洞,至少有超过50万个电子邮件服务器容易受到远程黑客攻击.Exim是一种广泛使用的开源邮件传输代理(MTA)软件,为类似 ...