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

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. 【HANA系列】SAP HANA跟我学HANA系列之创建分析视图一

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA跟我学HANA系 ...

  2. P1936 【水晶灯火灵】

    lalala~~(才不会告诉你这是题面呢) 这题确实有点坑,第一遍穷举超时,然后就开始了漫漫找规律之路... 终于,在经过5分钟的纠结之后,我终于发现了这个神奇的规律,那就是 Fabonacci!!! ...

  3. Java基础/Java异常

    Java异常 1.异常的分类: ① 非运行时异常(Checked Exception) Java中凡是继承自Exception但不是继承自RuntimeException的类都是非运行时异常 ② 运行 ...

  4. SpringBoot项目集成cas单点登录

    添加依赖 添加cas client依赖 <dependency> <groupId>net.unicon.cas</groupId> <artifactId& ...

  5. excel常用公式--逻辑运算类

    if:  IF(logical_test, value_if_true, [value_if_false]). and: 逻辑判断,相当于“并”. or: 逻辑判断,相当于“或”.

  6. C语言作业Ⅰ12

    一.我学到的内容 二.我的收获 时间 作业链接 我的收获 第一周 https://www.cnblogs.com/deng9/p/11576196.html#4369234 让我对这个专业有了新的认识 ...

  7. HDU 1087 Super Jumping! Jumping! Jumping! (动态规划、最大上升子序列和)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. System.InsufficientMemoryException:无法分配536870912字节的托管内存缓冲区。可用内存量可能不足

    一个病人住院太久,一次性打印护理表单超过3000条时报如标题所示的错误, 个人查阅分析应该可以从如下几方面入手: 一:查看程序客户端和服务端的配置文件相关属性是否限制了缓存最大值 (应该不是这个问题, ...

  9. HDU-5201 The Monkey King

    题目描述 \(m\)个猴子分\(n\)个桃,要求第一个猴子的桃数严格大于其他猴子,问有多少种分法对\(1e9+7取模(\%1e9+7)\) Input \(1≤T≤25 ,1≤n,m≤100000\) ...

  10. 解决PHP上传文件、下载文件中由于文件过大导致的上传失败及下载不全问题

    用php+apache上传文件的时候,由于文件过大,容易导致上传失败, 解决办法:修改php.ini中:upload_max_filesize  2m  即允许上传文件大小的最大值.默认为2M ,大小 ...