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) ...
随机推荐
- 一:jvm的五大内存区(内存结构)
jvm五大内存区域(即jvm运行时数据区),描述的是类被加载时,经过解析后,存储到特定的数据区.方法区和堆是所有线程共享的,而栈和计数器是线程私有的.栈处理程序运行的问题,堆处理数据的存储问题.所以才 ...
- 02-Django-views
# views 视图# 1. 视图概述- 视图即视图函数,接收web请求并返回web响应的事物处理函数.- 响应指符合http协议要求的任何内容,包括json,string, html等 # 2 其他 ...
- oracle数据库中的存储过程
存储过程是一组为了完成特定功能的sql语句集,是一段sql代码片段,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果存储过程存在参就给出,不存在就不用给出参数)来执行它.因为它是一段 ...
- loadkeys - 调入键盘翻译表
总览 (SYNOPSIS) loadkeys [ -d --default ] [ -h --help ] [ -q --quiet ] [ -v --verbose [ -v --verbose ] ...
- UVa11806 Cheerleaders(容斥原理)
11806 - Cheerleaders Time limit: 2.000 seconds C Cheerleaders In most professional sporting events, ...
- 如何获得带转义的json内容
stringify两次 JSON.stringify(JSON.stringify(obj))
- linux下vim如何清空一个文件?
这是一个很巧妙的方法.如何来清空一个文件里的内容呢! 很简单,但确很实用: echo " " > filename(文件名称); 一句话就可以搞定.
- AtomicReference和AtomicStampedReference
AtomicReference类提供了一个可以原子读写的对象引用变量. 原子意味着尝试更改相同AtomicReference的多个线程(例如,使用比较和交换操作)不会使AtomicReference最 ...
- java实现一个简单的计数器
package com.fengunion.sf; import org.junit.platform.commons.util.StringUtils; import java.util.HashM ...
- STM32CubeMX FreeRTOS no definition for "osThreadGetState" 解决办法
用STM32CubuMX默认加入的FreeRTOS默认配置eTaskGetState是禁止的 把该功能设为Enabled编译就不会出错了 IAR的编译器要勾选Allow VLA