题目:

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

  1. [[0,0,1,0,0,0,0,1,0,0,0,0,0],
  2. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  3. [0,1,1,0,1,0,0,0,0,0,0,0,0],
  4. [0,1,0,0,1,1,0,0,1,0,1,0,0],
  5. [0,1,0,0,1,1,0,0,1,1,1,0,0],
  6. [0,0,0,0,0,0,0,0,0,0,1,0,0],
  7. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  8. [0,0,0,0,0,0,0,1,1,0,0,0,0]]
    Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
  1. //参考了答案……思路有,但是dfs写的不对………………
  2. class Solution {
  3. public:
  4. int maxAreaOfIsland(vector<vector<int>>& grid) {
  5. int res = 0;
  6. for (int i = 0; i < grid.size(); i++)
  7. {
  8. for (int j = 0; j < grid[0].size(); j++)
  9. {
  10. if (grid[i][j])
  11. {
  12. res = max(res, dfs(grid, i, j));
  13. }
  14. }
  15. }
  16. return res;
  17. }
  18.  
  19. int dfs(vector<vector<int>>& grid,int i,int j)
  20. {
  21. if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size()) return 0; //超出边界返回0
  22. if (grid[i][j])
  23. {
  24. grid[i][j] = 0;
  25. return 1 + dfs(grid, i, j + 1) + dfs(grid, i + 1, j)
  26. + dfs(grid, i, j - 1) + dfs(grid, i - 1, j); //边界内,且为1,返回XXX
  27. }
  28. return 0; //不是1 返回0
  29. }
  30. };

【easy】695. Max Area of Island的更多相关文章

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

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

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

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

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

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

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

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

  9. 695. Max Area of Island

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

随机推荐

  1. Steps to One DP+莫比乌斯反演

    卧槽,这么秀吗??? 暂时留坑...

  2. docker(二) windows10下安装docker

    官方安装文档: https://docs.docker.com/docker-for-windows/install/ https://docs.docker.com/docker-for-windo ...

  3. Oracle SQLULDR2 以及 SQLLDR 进行导入导出的功能说明

    Study From http://blog.itpub.net/28291944/viewspace-2142187/ 自己尝试了下 可以实现. 下载完sqluldr2,文件夹内容如下:sqluld ...

  4. deepin配置Oracle JDK

    这里记录一下入手deepin后,安装JDK的过程,和之前的CentOS有些不同 本篇参考了两篇博客 1 2 第一篇有些问题,在第二篇中找到了解决方案 接下来是操作过程: 检查本机自带的OpenJDK, ...

  5. python之生成器和列表推导式

    一.生成器函数 1.生成器 就是自己用python代码写的迭代器,生成器的本质就是迭代器(所以自带了__iter__方法和__next__方法,不需要我们去实现). 2.构建生成器的两种方式 1,生成 ...

  6. 一分钟学会JavaMail(假)__手动滑稽

    因为公司内部办公系统(OA)需要增加一个发送邮件的功能,所以学习了这个感觉比较冷门的JavaMail   1.先上成功截图 : 2.准备事项:Java Mail虽然是官方写的,但是没有集成到jdk里面 ...

  7. supervisor进程管理的使用

    介绍 Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.它是通过fork/exec的方式把这些被管理 ...

  8. BZOJ4817[Sdoi2017]树点涂色——LCT+线段树

    题目描述 Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色.Bob可能会进 ...

  9. 使用Sublime Text 3进行Markdown编辑+实时预览

    使用Sublime Text 3进行Markdown编辑+实时预览 安装软件包管理器 打开Sublime Text 3 同时按下 ctrl+` ,窗口底部出现一个小控制台 复制以下代码,粘贴到控制台的 ...

  10. Count on a tree SPOJ - COT (主席树,LCA)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...