国庆中秋长假过完,又要开始上班啦。先刷个题目找找工作状态。

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.
Note: The length of each dimension in the given grid does not exceed 50.

解题思路:本题属于简单级别,但是我还是拿出来分析。因为本题我们可以用广度遍历的算法解决,而广度遍历也是算法中非常重要的一种。步骤如下:

1.从输入的2D数组首元素开始遍历,如果值为0,继续下一个节点;否则,将这个节点入中间过程栈。

2.遍历中间过程栈,将栈中每个节点的上下左右相邻的并且值为1的节点继续入中间过程栈,并计数(这个计数就是表示Area of Island的大小)。注意,每次从栈中取一个新的节点,需要用一个数组记录这个节点是否已经被访问过,保证每个节点只会在中间过程栈中出现一次;同时,对于计数过的节点也要用数组记录,确保每个节点也只能计数一次。

3.如果中间过程栈为空,则继续步骤1,直到2D数组遍历完成。

代码如下:

class Node:
def __init__(self,x,y):
self.x = x
self.y = y
class Solution(object):
sk = []
def appendNode(self,x,y):
for i in self.sk:
if i.x == x and i.y == y:
return
self.sk.append(Node(x,y))
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
visit = [] #record a node if already visisted
counted = [] #record a node if already counted
width = len(grid[0])
height = len(grid)
for i in grid:
l = []
for j in i:
l.append(0)
visit.append(l)
counted.append(l)
maxCount = 0
for i in range(height):
for j in range(width):
if visit[i][j] == 1:
continue
if grid[i][j] == 0:
continue
n = Node(i,j)
self.sk.append(n)
count = 1
while len(self.sk) > 0:
x = self.sk[0].x
y = self.sk[0].y
if x+1 < height and visit[x+1][y] == 0 and grid[x+1][y] == 1:
if counted[x+1][y] == 0:
count += 1
counted[x+1][y] = 1
self.appendNode(x+1,y)
if x-1 >= 0 and visit[x-1][y] == 0 and grid[x-1][y] == 1:
if counted[x-1][y] == 0:
count += 1
counted[x-1][y] = 1
self.appendNode(x-1,y)
if y-1 >= 0 and visit[x][y-1] == 0 and grid[x][y-1] == 1:
if counted[x][y-1] == 0:
count += 1
counted[x][y-1] = 1
self.appendNode(x,y-1)
if y+1 <width and visit[x][y+1] == 0 and grid[x][y+1] == 1:
if counted[x][y+1] == 0:
count += 1
counted[x][y+1] = 1
self.appendNode(x,y+1)
visit[x][y] = 1
del self.sk[0]
if maxCount < count:
maxCount = count
return maxCount

【leetcode】Max Area of Island的更多相关文章

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

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

  3. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  4. 【leetcode】Max Points on a Line(hard)☆

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

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

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

  6. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

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

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

  8. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. C++中的类型识别

    1,为什么会提出类型识别概念呢? 1,为什么在 C 语言中没有提出这个概念呢,就是因为在 C++ 中引入了面向对象的特性,面向对象里面有一个非常重要的原则就是赋值兼容性原则: 2,在面向对象中可能出现 ...

  2. redis 列表 数据类型

    列表 rpush dname  技术部  后勤部 售后部 lpush  dname   秘书部 lset dname 2  销售部     修改 lrange dname 0 -1   打印所有列表 ...

  3. python中几个常见的魔法方法

    首先,什么是魔法方法呢?在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做"魔法"方法. __ init__()方法 当一个实例被创建的时候调用的初始 ...

  4. 如何用vue封装一个防用户删除的平铺页面的水印组件

    需求 为了防止截图等安全问题,在web项目页面中生成一个平铺全屏的水印 要求水印内容为用户名,水印节点用户不能通过开发者工具等删除 效果 如上图 在body节点下插入水印DOM节点,水印节点覆盖在页面 ...

  5. CS起源:实现狙击子弹加速

    在前面的课程 FPS 游戏实现方框透视 中我们实现了对CS中游戏人物的透视效果,今天我们就来研究下狙击枪如何变成机关枪!原理很简单,直接去掉枪的上膛动画,配合无线子弹就完事了,这里只提供一种分析思路. ...

  6. 移动端 h5 适配之必知必会

    建议大家先去看看这篇文章 https://juejin.im/post/5cddf289f265da038f77696c?utm_source=gold_browser_extension(来自掘金: ...

  7. 20.AutoMapper 之理解你的映射(Understanding Your Mappings)

    https://www.jianshu.com/p/4f5c14fbf1c2 理解你的映射(Understanding Your Mappings) AutoMapper 为你的映射创建执行计划.在调 ...

  8. Linux运维必会的100道MySql面试题之(一)

    01 如何启动MySql服务 /etc/init.d/mysqld start service mysqld start Centos 7.x 系统 sysctl  start mysqld 02 检 ...

  9. MySQL on duplicate key update 批量插入并更新已存在数据

    业务上经常存在一种现象,需要批量往表中插入多条数据,但在执行过程中,很可能因为唯一键冲突,而导致批量插入失败.因此需要事先判断哪些数据是重复的,哪些是新增的.比较常用的处理方法就是找出已存在的数据,并 ...

  10. 微信小程序环境搭建(本地,测试,生产)

    1.本地 官网文档链接:https://cloud.tencent.com/document/product/619/11442#.E6.9C.AC.E5.9C.B0.E5.A6.82.E4.BD.9 ...