【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) 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.)
[[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, return6. Note the answer is not 11, because the island must be connected 4-directionally.
//参考了答案……思路有,但是dfs写的不对………………
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res = 0;
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[0].size(); j++)
{
if (grid[i][j])
{
res = max(res, dfs(grid, i, j));
}
}
}
return res;
} int dfs(vector<vector<int>>& grid,int i,int j)
{
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size()) return 0; //超出边界返回0
if (grid[i][j])
{
grid[i][j] = 0;
return 1 + dfs(grid, i, j + 1) + dfs(grid, i + 1, j)
+ dfs(grid, i, j - 1) + dfs(grid, i - 1, j); //边界内,且为1,返回XXX
}
return 0; //不是1 返回0
}
};
【easy】695. Max Area of Island的更多相关文章
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 695. Max Area of Island@python
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 ...
- 695. Max Area of Island
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- Redis详解(三)------ redis的五大数据类型详细用法
我们说 Redis 相对于 Memcache 等其他的缓存产品,有一个比较明显的优势就是 Redis 不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据 ...
- Math的一些方法
Math.abs(数值) 把()内的值变为正数 Math.ceil(4.3) 向上取整 // 5 Math.floor(4.3) 向下取整 // 4 Math.round(4.3) 四舍五入取整 // ...
- VS2010安装MVC4记录
想学MVC4,但自己电脑上是VS2010,所以在网上查询怎样安装MVC4,总结下给大家. VS2010默认安装的是MVC2,安装MVC4必须先装 SP1,然后安装MVC4. VS2010 SP1 (1 ...
- 软件工程(GZSD2015) 第二次作业文档模板
题目: (此处列出题目) 需求分析: 基本功能 基本功能点1 基本功能点2 ... 扩展功能(可选) 高级功能(可选) 设计 设计点1 设计点2 ... 代码实现 // code here 程序截图 ...
- 搭建Android手机服务器(一)
自从上学期的软件工程课之后,我们团队设计的抢答器一直想把服务端移到移动端.所以,我一直在考虑如何把手机作为一台服务器.今天我主要讲解一下,对于没有android真机只有模拟器的,如何设置模拟器,使得在 ...
- 一些有意思的Linux命令
1.输出你最常用的十条命令 history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort - ...
- 洛谷P4155 BZOJP4444 [SCOI2015]国旗计划
自己做出来的第一道倍增(fake) 原题链接 看到题目,贪心水题!不仅思维难度低,代码也好(难)写,三下五除二就写了出来,过了样例.但是一交,只有40pts.一道紫题能让我快速水40pts,良心出题人 ...
- Cucumber环境配置
1.JDK下载和配置 2.Maven下载和环境配置 3.IDEA下载和Cucumber插件安装 下载地址官网,一般IDEA自带cucumber插件 如果没有安装过的话,点击以下进行搜索安装,安装完成后 ...
- 剑指Offer_编程题_20
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. /* struct TreeNode { int val; struct TreeNode *left; struct TreeN ...
- 分布式监控系统开发【day38】:报警模块解析(六)
一.负责把达到报警条件的trigger进行分析 ,并根据 action 表中的配置来进行报警 1.目录结构 2.功能如下 1.找到trigger的关联动作, 2.收到的数据传给trigger_msg就 ...