给定一个包含了一些 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;}
};

转载于:https://www.cnblogs.com/lMonster81/p/10434004.html

Leetcode695.Max Area of Island岛屿的最大面积相关推荐

  1. [Leetcode] Max Area of Island 最大岛屿面积

    Max Area of Island 最新更新请见:https://yanjia.me/zh/2019/02/... Given a non-empty 2D array grid of 0's an ...

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

  3. 695. Max Area of Island (Medium)——岛屿的最大面积

    前言: 本题目为深度优先遍历(DFS) 算法的一道典型例题. 题目 : 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成 ...

  4. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

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

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

  6. leetcode 695. Max Area of Island | 695. 岛屿的最大面积(DFS)

    题目 https://leetcode.com/problems/max-area-of-island/ 题解 class Solution {int M, N;public int maxAreaO ...

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

  8. LeetCode - 695. Max Area of Island (Java)

    R.C记录矩阵行列 可以将邻接矩阵转为邻接表来做,即要将二维数组转换为一维数组: 将二维坐标转化为一维坐标: V = x * C + y 若将一维坐标转化为二维坐标: x = V / C y = V ...

  9. LeetCode 695. Max Area of Island javascript解决方案

    题意: 寻找最大岛. leetcode.com/problems/ma- 传入: [[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], ...

最新文章

  1. HashMap内部结构深入剖析
  2. css中单位px和em,rem的区别
  3. 【9.22校内测试】【可持久化并查集(主席树实现)】【DP】【点双联通分量/割点】...
  4. Python-day-9- RabbitMQ队列
  5. Linux软件安装配置Yum源
  6. brtools备份与恢复
  7. 常用SQL语句书写技巧-
  8. [深度学习基础] 深度学习基础及数学原理
  9. kail linux稳定版本,Kali Linux 2020.3 稳定版已发布 更新后新功能概览
  10. 【ES6基础】Object的新方法
  11. lempel ziv matlab,基于Python的LempelZiv算法的熵估计
  12. java正则过滤js_JS/Java正则表达式验证
  13. java clone 深拷贝_Java clone() 浅拷贝 深拷贝
  14. php实现语音留言,iPhone实现语音留言 新技能get
  15. Unity3D 热更新方案(集合各位专家的汇总)
  16. (转)android拨打电话崩溃6.0以上实时动态权限申请
  17. PMP考试中一些解题思路
  18. 软件测试带宽低,性能测试分析之带宽瓶颈的疑惑
  19. 人像姿势,从细节做起!
  20. oracle 控制台使用手册,Oracle-ESS-入门手册

热门文章

  1. 一文读懂 YOLOv1,v2,v3,v4 发展史
  2. 作曲与计算机音乐,论计算机音乐与计算机作曲
  3. 深度神经网络在光通信及数字信号处理领域的应用总结(AI-ODSP)(二)
  4. 学习嵌入式必读十本书,从C语言到ARM
  5. 超全面!信息图形设计知识全方位科普
  6. html格式转换成 视频格式,PPT转成视频格式方法
  7. JSPatch使用小记
  8. 一招教你学会水仙花数
  9. 世嘉新人培训教材学习_第一章 (推箱子代码)
  10. 有关树的常见算法汇总【持续更新中】