这个题使用深度优先搜索就可以直接遍历

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 岛的最大面积的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 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 用了第一种方式, ...

  5. [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 ...

  6. 【leetcode】Max Area of Island

    国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...

  7. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  8. 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 ...

  9. 【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) ...

随机推荐

  1. Redux 关系图解

    Redux是一款状态管理库,并且提供了react-redux库来与React亲密配合, 但是总是傻傻分不清楚这2者提供的API和相应的关系.这篇文章就来理一理. Redux Redux 三大核心 Re ...

  2. Django 调试models 输出的SQL语句 定位查看结果

    django 调试models变得更为简单了,不用像之前的版本, 手工去调用django query, 才能打印出之前的代码是执行的什么SQL语句. 1.3开始只需在settings.py里,配置如下 ...

  3. Could not locate executable null\bin\winutils.exe in the Hadoop binaries解决方式

    1.  问题: 2.  问题解决: 仔细查看报错是缺少winutils.exe程序. Hadoop都是运行在Linux系统下的,在windows下eclipse中运行mapreduce程序,要首先安装 ...

  4. SpringBoot_04springDataJPA

    说明:底层使用Hibernate 一.springDataJPA和mybatisPlus的使用区别 第一步: 把mybatisPlus的依赖.配置删除 包括:实体类的注解.引导类的mapperScan ...

  5. 【学习总结】快速上手Linux玩转典型应用-第7章-WebServer安装和配置讲解

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. Apache的安装 2. Apache的虚拟主机配置及伪静态操作 3. Nginx的基本操作 4. Nginx伪静态的实现 5. 实例演 ...

  6. js放大镜特效

    在平时网上商城购物时,我们能够通过放大镜效果来使我们看图片能够更加的清楚,今天我就来给大家分享一下我学习的放大镜特效 下图是原图的样子                                 ...

  7. Trait讲解

    <?php /** * Trait解决PHP单继承的一种方法,使开发人员在不同层次结构的类中复用属性和方法 * Trait无法实例化 * Trait不是类,不能被继承,所以不能再Trait中不能 ...

  8. Nginx 配置状态信息虚拟主机

    可以在浏览器中查看并发数量 [root@Liangenyu conf]# vim nginx.conf server { listen 80; server_name status.etiantian ...

  9. mdoc.samples - 用 -mdoc 编写 BSD 手册 的 示范教程

    SYNOPSIS (总览) man mdoc.samples DESCRIPTION (描述) 这个 示范教程 用于 编写 BSD 手册页 (manual page), 它 使用了 -mdoc 宏定义 ...

  10. STM32F407 正点原子按键输入实验

    库函数版本: 库函数 源文件 头文件 GPIO_Init(GPIOE, &GPIOE_initstructure) stm32f4xx_gpio.c stm32f4xx_gpio.h RCC_ ...