给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。

找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)

示例 1:

[[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]]

对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。

示例 2:

[[0,0,0,0,0,0,0,0]]

对于上面这个给定的矩阵, 返回 0。

注意: 给定的矩阵grid 的长度和宽度都不超过 50。

BFS:

class Solution {
public:
vector<vector<int> > visit;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r;
int c;
int maxAreaOfIsland(vector<vector<int> >& grid) {
r = grid.size();
if(r == 0)
return 0;
c = grid[0].size();
visit = vector<vector<int> >(r, vector<int>(c, 0));
int res = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(visit[i][j] != 1 && grid[i][j] == 1)
{
visit[i][j] = 1;
res = max(res, BFS(grid, i, j));
}
}
}
return res;
} int BFS(vector<vector<int> >& grid, int x, int y)
{
queue<pair<int, int> > q;
q.push(make_pair(x, y));
int cnt = 0;
while(!q.empty())
{
int xx = q.front().first;
int yy = q.front().second;
cnt++;
q.pop();
for(int i = 0; i < 4; i++)
{
int newx = xx + dx[i];
int newy = yy + dy[i];
if(newx < 0 || newx >= r || newy < 0 || newy >= c)
continue;
if(visit[newx][newy] == 1)
continue;
if(grid[newx][newy] == 0)
continue;
visit[newx][newy] = 1;
q.push(make_pair(newx, newy));
}
}
return cnt;
}
};

DFS:

class Solution {
public:
vector<vector<int> > visit;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r;
int c;
int maxAreaOfIsland(vector<vector<int> >& grid) {
r = grid.size();
if(r == 0)
return 0;
c = grid[0].size();
visit = vector<vector<int> >(r, vector<int>(c, 0));
int res = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(visit[i][j] != 1 && grid[i][j] == 1)
{
res = max(res, DFS(grid, i, j));
}
}
}
return res;
} int DFS(vector<vector<int> >& grid, int x, int y)
{
int cnt = 1;
visit[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if(newx < 0 || newx >= r || newy < 0 || newy >= c)
continue;
if(visit[newx][newy] == 1)
continue;
if(grid[newx][newy] == 0)
continue;
cnt += DFS(grid, newx, newy);
}
return cnt;
}
};

Leetcode695.Max Area of Island岛屿的最大面积的更多相关文章

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

  2. leetcode 695 Max Area of Island 岛的最大面积

    这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...

  3. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  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. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...

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

  7. 【leetcode】Max Area of Island

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

  8. [Swift]LeetCode695. 岛屿的最大面积 | 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 ...

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

随机推荐

  1. 在AlexNet中LRN 局部响应归一化的理

    在AlexNet中LRN 局部响应归一化的理 一.LRN技术介绍: Local Response Normalization(LRN)技术主要是深度学习训练时的一种提高准确度的技术方法.其中caffe ...

  2. 安装配置git服务

    创建git用户和组 groupadd -g git useradd -md /home/git -g -u git 安装依赖包 yum install curl-devel expat-devel g ...

  3. Vue简单评星效果与单张图片上传

    <form class="" id="pj-frm"> <div class="assess-header"> &l ...

  4. UITableViewHeaderFooterView can't change custom background when loading from nib

    down voteforite I've created a custom UITableViewHeaderFooterView and successfully load from nib int ...

  5. spring-jdbc-aop事务

    1 spring整合JDBC 1.1 概述 spring提供了很多模板整合Dao技术  spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => J ...

  6. js 高亮显示关键字

    示例: var defaultEmphasisHandler = function(keyword, data){ var regex = RegExp("("+keyword.r ...

  7. Dom直接选择器

    Dom直接选择器 <!DOCTYPE html> <!--Dom间接选择器--> <html lang="en"> <head> & ...

  8. SSM11-solr服务的搭建

    1.  Solr服务搭建 1.1. Solr的环境 Solr是java开发. 需要安装jdk. 安装环境Linux. 需要安装Tomcat. 1.2. 搭建步骤 第一步:把solr 的压缩包上传到Li ...

  9. H5C3--属性选择器、兄弟选择器、伪类选择器

    属性选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  10. localStorage对象简单应用 - - 访问次数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...