【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 ...
随机推荐
- 保护 .NET Core 项目的敏感信息
我们的项目中几乎都会有配置文件,里面可能会存储一些敏感信息,比如数据库连接字符串.第三方 API 的 AppKey 和 SecretKey 等. 对于开源项目,这些敏感信息肯定不能随着源代码一起提交到 ...
- 对于for循环中使用let或var时,i的作用域范围的记录
在for循环中使用let时,结果如下 for内部定义的i在循环结束后不会覆盖外部的i 在for循环中使用var,且不控制i的作用域时,结果如下 第一个for循环内部定义的i并不会创建,而是直接使用外部 ...
- SQL Server没有足够的内存继续执行程序 (mscorlib)的解决办法
在Microsoft SQL Server Management Studio 中执行较大的sql脚本时,会报没有足够的内存继续执行程序(mscorlib)的错误.如下图所示 解决方法: 使用sqlc ...
- 数据标记系列——标记工具Imagtagger
https://github.com/bit-bots/imagetagger 待有空说一说!
- [SimplePlayer] 3. 视频帧同步
Frame Rate 帧率代表的是每一秒所播放的视频图像数目.通常,视频都会有固定的帧率,具体点地说是每一帧的时间间隔都是一样的,这种情况简称为CFR(Constant Frame Rate);另外一 ...
- 测试常用Linux命令
大家应该经常在网络上看到下图吧,虽然我们不会去执行下面图片中的命令,但是linux常用的命令对于测试人员来说,还是必须掌握的,不管是做功能测试还是性能测试,最常用的就是看日志了. sudo是linux ...
- 和CISSP并肩的信息安全认证国际注册信息安全经理CISM
众所周知,信息安全认证界有一个扛把子的证书叫CISSP(国际信息安全专家认证),一般拥有CISSP证书的小哥哥还会选择考取另一个认证,这就是今天给大家介绍的CISM(国际注册信息安全经理).CISM是 ...
- LeetCode刷题(Java)
第一题 class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = ...
- 2017-12-19python全栈9期第四天第一节之昨日内容回顾与作业讲解之插入insert和extend
#!/user/bin/python# -*- coding:utf-8 -*-li = ['zs','ls','ww','zl']li.insert(4,'cc')print(li)li.exten ...
- 关于ddl(新增字段)对数据库锁表|读写操作的影响_资料
1.对一个表执行ddl(新增字段)会不会阻塞表,影响读写? 在一次项目升级之前需要执行一个新增字段的脚本(alter table...),表的数据量是260多万,执行时间是72秒,感觉略长,不知道会不 ...