[抄题]:

求最多的联通的1的数量

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

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

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

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

Given the above grid, return 0.

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

  1. 以为棋盘问题都是向四周扩展、bfs,其实本质上不是对棋盘元素操作,本质上是求数量最大,还是DFS先求所有
  2. 图中居然也能用二叉树的traverse嵌套,头一次见

[一句话思路]:

某点的面积是由四周的点构成的,四周的点的面积又是由四周的点构成的,所以用traverse递归嵌套。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

为防止重复计算,把1的点先标记为0,使其不再符合条件。第一次见。

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

没看出来把= 写成 == 了,不应该

[总结]:

本质上不是对棋盘元素操作,本质上是求数量最大,还是DFS先求所有

[复杂度]:Time complexity: O(n2) Space complexity: O(n2)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

本质上不是对棋盘元素操作,本质上是求数量最大,还是DFS先求所有

[关键模板化代码]:

public int areaOfIsland(int i, int j, int[][] grid) {
//valid first, == 1 second
if (0 <= i && i < grid.length && 0<= j && j < grid[0].length && grid[i][j] == 1) {
//restore to 0 to avoid repeat
grid[i][j] = 0;
//count area
return 1 + areaOfIsland(i - 1, j, grid) + areaOfIsland(i + 1, j, grid) + areaOfIsland(i, j - 1, grid) + areaOfIsland(i, j + 1, grid);
}
//if not 1, default case : return 0
return 0;
}

traverse嵌套

[其他解法]:

并查集,太麻烦了

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int maxAreaOfIsland(int[][] grid) {
//corner case
if (grid.length == 0 || grid[0].length == 0) {
return 0;
}
//compare all areas
int max = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
max = Math.max(max, areaOfIsland(i, j, grid));
}
}
return max;
} public int areaOfIsland(int i, int j, int[][] grid) {
//valid first, == 1 second
if (0 <= i && i < grid.length && 0<= j && j < grid[0].length && grid[i][j] == 1) {
//restore to 0 to avoid repeat
grid[i][j] = 0;
//count area
return 1 + areaOfIsland(i - 1, j, grid) + areaOfIsland(i + 1, j, grid) + areaOfIsland(i, j - 1, grid) + areaOfIsland(i, j + 1, grid);
}
//if not 1, default case : return 0
return 0;
}
}

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

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

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

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

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

  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 解题报告(Python & C++)

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

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

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

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

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

随机推荐

  1. fileupload页面跳转找不到原页面的解决方法

    做了个上传图片的功能,之前做的全都对,完全可以实现,但是后来再弄的时候,只要FileUpload控件里面有字(选择了图片),再按button.它尽然不执行button1_click事件,直接页面跳转, ...

  2. 02 - Unit01:服务器返回数据的json处理+搭建项目环境

    服务器返回数据的json处理+搭建项目环境 服务器返回数据的json处理 springMVC JSP响应流程 请求 -->DispatcherServlet -->HandlerMappi ...

  3. C#操作MySql数据库帮助类(Dapper,T-Sql)

    using System.Text; using MySql.Data.MySqlClient; using System.Data; using Dapper; using System.Refle ...

  4. Java 中的this关键字

    1.this关键字代表当前对象 this.属性 操作当前对象的属性 this.方法 调用当前对象的方法 2.封装对象的属性的时候,经常会使用this关键字 public class Telphone ...

  5. python3第一次作业

    需要一个文件users,里面有用户名密码以及是否锁定的标识符 lzd--123--1wdl--123--0lw--123--0aaa--123--0bbb--123--0ccc--123--1ddd- ...

  6. 003:MySQL账号创建授权以及Workbench

    目录 一. 权限管理 1."用户 + IP"的概念 2. 用户权限管理 3. 基本操作 4. 撤销权限 5.授权和创建用户 二. MySQL模拟角色 三. Workbench与Ut ...

  7. Java复习——枚举与注解

    枚举 枚举就是让某些变量的取值只能是若干固定值中的一个,否则编译器就会报错,枚举可以让编译器在编译阶段就控制程序的值,这一点是普通变量无法实现的.枚举是作为一种特殊的类存在的,使用的是enum关键字修 ...

  8. 如何扩大重做日志(redolog)文件的大小

    假设现有三个日志组,每个组内有一个成员,每个成员的大小为1MB,现在想把此三个日志组的成员大小都改为10MB 1.创建2个新的日志组alter database add logfile group 4 ...

  9. 使用CCNode作为容器容易踩的坑

    Cocos2dx中CCNode经常作为一个父容器,里面装一些UI控件,最后组成一个复杂的自定义的UI控件,但是在使用别人的自定义控件和自己写自定义问题的时候会踩一些坑. 首先拿到一个自定义的UI控件一 ...

  10. 19_java之List和Set

    01List接口的特点 A:List接口的特点: a:它是一个元素存取有序的集合. 例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的). b:它是一 ...