【leetcode】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) 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的更多相关文章
- 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 ...
- [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 ...
- 【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 ...
- 【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. ...
- leetcode 695 Max Area of Island 岛的最大面积
这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)
Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- java游戏服务器--简单工厂模式
先来学习下简单工厂模式! 我们知道在游戏里有很多的场景,例如:帮派场景,副本场景,野外场景... 现在我们有这样的需求: 1.我们需要进入帮派场景时---开始执行帮派任务. 2.我们需要进入副本场景时 ...
- 【github】git常用命令和node项目
这是之前在本地搞的一个小东西.因为之前电脑经常会很卡.后来发现任务管理器中多了两个不知名的进程:chromgpu.exe和flrefox.exe 乍一看以为和chrome和firefox两个浏览器有关 ...
- python学习之面向对象(四)
6.9 反射 反射是非常重要的内置函数之一. 反射是通过字符串去操作对象相关的属性,这里的对象包括:实列,类,当前模块,其他模块. 主要的四个函数: 查看: hasattr(对象,待查内容的字符串形式 ...
- poj1220(短除法实现任意进制转换)
题目链接:https://vjudge.net/problem/POJ-1220 题意:给定a进制的大数s,将其转换为b进制.其中2<=a,b<=62. 题意:一般进制转换是以10进制为中 ...
- Websocket --(2)实现
首先声明,本篇博文参考文章 https://blog.csdn.net/jack_eusong/article/details/79064081 主要在于理解和自己动手搭建环境,自己搭建的过程中会发生 ...
- 7大python 深度学习框架的描述及优缺点绍
Theano https://github.com/Theano/Theano 描述: Theano 是一个python库, 允许你定义, 优化并且有效地评估涉及到多维数组的数学表达式. 它与GPUs ...
- 【6.18校内test】T3细胞分裂
尽管T1T2很简单,但还是阻止不了我T3wa一片 细胞分裂[题目链接] xcg同学有一个80pts的代码 他说他的代码和我的很像,可惜我比较笨,只有30pts 其实这道题考场上是想到要分解质因数了,然 ...
- hadoop-mapreduce 详解
mapreduce 完整流程解析 1. 在客户端启动一个 job: hadoop jar share/hadoop/tools/lib/hadoop-streaming-2.6.5.jar -file ...
- 剑指offer 数字翻译成字符串
0 -> 'a', 1->'b', ..., 11 -> 'l', ..., 25->'z'. 计算一个数有多少种不同的翻译方法. 分析:记f[i]表示从第i位起的不同翻译数目 ...
- 帝国cms 项目搬家换域名修改详情页图片路径
update phome_ecms_news_data_1 set newstext=REPLACE (newstext,'/d/file/','http://www.xxxx.com/d/file/ ...