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) ...
随机推荐
- 一分钟搭建 Web 版的 PHP 代码调试利器
一.背景 俗话说:"工欲善其事,必先利其器".作为一门程序员,我们在工作中,经常需要调试某一片段的代码,但是又不想打开繁重的 IDE (代码编辑器).使用在线工具调试代码有时有 ...
- ArcGIS Server导出shp文件
需求: 在项目中客户提出需要在Web端能够定义条件将后台的数据导出shp文件,并下载. 实现: 基于ArcGIS开发导出矢量数据的服务,用户输入导出数据类型.过滤条件.导出范围等条件,服务能够快速将相 ...
- Paper Reading_Database
最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 AI+DB A. Pavlo et al., Self-Driving Database Engi ...
- 通过修改host解决VS2019下载极慢的问题
原文:通过修改host解决VS2019下载极慢的问题 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...
- JavaEE高级-MyBatis学习笔记
一.MyBatis简介 - MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. - MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. - My ...
- 简单Spring Cloud 微服务框架搭建
微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ...
- At grand 022 GCD序列构造 dp/floyd二进制变换最少费用
A diverse words指的是每一个字母在单词中出现的次数不超过1的单词 题目要求你求出字典序比当前给定单词大的字典序最小单词 1.如果给定的单词长度小于26 就遍历一次在单词尾部加上字典序最小 ...
- socket客户端的备份机制
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0); //设定服务器的地址信息 SOCKADDR_IN addrSrv; addrSrv.sin_a ...
- Kettle整理
下载kettle版本 (1)hadoop version 查看hadoop的版本 hadoop2.6 (2)则在data-integration\plugins\pentaho-big-data ...
- 前端框架之BootStrap的简单介绍
Bootstrap补充 一.一个小知识点 1.截取长屏的操作 2.设置默认格式 3.md,sm, xs 4.空格和没有空格的选择器 二.响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现 ...