[抄题]:

求最多的联通的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. [LeetCode系列]BST有效性确定问题[前序遍历]

    给定一个BST的根节点, 试判断此BST是否为符合规则的BST? 规则: 对于一个BST的节点, 它左侧的所有节点(包括子节点)必须小于它本身; 它右侧的所有节点(包括子节点)必须大于它本身; 它的左 ...

  2. Fuel9.0安装openstack过程中所踩过的坑2018最新版

    坑一,安装好后,无法访问Web UI画面 访问https//10.20.0.2:8443无法打开UI画面.首先我们不管以后的步骤,打不开是很不爽的. 解决方法:把下面网卡1,网卡2,网卡3的界面名称都 ...

  3. (转)如何获得当前ListVIew包括下拉的所有数据?

    ListView listView = activity.getListView();获取的仅仅是当前屏幕显示的list,但是具有下拉信息,不在当前屏幕,但是下拉显示的数据无法或得到.谁知道如何获得当 ...

  4. CentOS部署NetCore - 2. 安装NetCore SDK On CentOS

    登录微软官网,进入Install .NET Core SDK on Linux CentOS / Oracle 按照对应的指令,安装SDK Install the .NET SDK Update th ...

  5. vss和vs2008组合搭建源代码管理器

    用源代码管理项目,是为了方便开发和管理组内项目,一个组做的是同一套项目,彼此知道各个模块的进度和开发情况,这也是开发项目所需要的.今天整理了VSS的安装.创建.连接及添加项目等操作. 一.安装VSS( ...

  6. 学习 FPGA之前的基础知识

    在学习一门技术之前往往应该从它的编程语言入手,比如学习单片机时,往往从汇编或者C语言入门.所以不少开始接触FPGA的开发人员,往往是从VHDL或者Verilog开始入手学习的.但小编认为,若能先结合& ...

  7. Java 输入一个整数,计算它各位上数字的和。(注意:是任意位的整数)

    import java.util.*; /* * 输入一个整数,计算它各位上数字的和. * (注意:是任意位的整数) */ public class Sum02 { public static voi ...

  8. Log4j配置详解之log4j.xml

    Log4j配置详解之log4j.xml Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息. ...

  9. Go - 指针简介 与 ++/--运算符以及控制语句

    指针 Go 语言中,对于指针有一些特殊约束: 1. 不在支持 “->” 符号,所有的指针使用“.” 来操作指针对象的成员变量 2. 指针的默认值为 “nil” ++ 与 -- 作为语句而非表达式 ...

  10. 【洛谷】P1196 银河英雄传说(并查集)

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...