【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 ...
随机推荐
- 自动部署脚本-bash
from here !/bin/bash Check if user is root if [ $(id -u) != "0" ]; then Echo_Red "Err ...
- 【计算机视觉】【图像处理】【VS开发】【Qt开发】opencv之深拷贝及浅拷贝,IplImage装换为Mat
原文:opencv之深拷贝及浅拷贝,IplImage装换为Mat 一.(1) 浅拷贝: Mat B; B = image // 第一种方式 Mat C(image); // 第二种方式 这两种方式称 ...
- 一、Kubernetes_V1.10集群部署-master-生成证书
一.证书生成 1.下载cfssl mkdir -p /etc/kubernetes/sslwget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget ...
- MySQL数据库常用引擎
在MySQL数据库中,常用的引擎主要就是2个:Innodb和MyIASM. 首先: 1.简单介绍这两种引擎,以及该如何去选择.2.这两种引擎所使用的数据结构是什么. 1. a.Innodb引擎,Inn ...
- python-2:爬取某个网页(虎扑)帖子的标题做词云图
关键词:requests,BeautifulSoup,jieba,wordcloud 整体思路:通过requests请求获得html,然后BeautifulSoup解析html获得一些关键数据,之后通 ...
- Codeforces 1220B. Multiplication Table
传送门 冷静分析容易发现,我们只要能确定一个数的值,所有值也就可以确定了 确定一个数的值很容易,$a_ia_j=M_{i,j},a_ia_k=M_{i,k},a_ja_k=M_{j,k}$ 然后就可以 ...
- P1397 [NOI2013]矩阵游戏
传送门 首先显然可以矩乘快速幂然后 $T$ 飞 看一眼题解发现因为这一题矩阵的特殊性所以可以对矩阵的次数欧拉降幂 然而我并不懂证明,所以我选择暴力乱搞的做法 十进制快速幂,然后注意一下常数,还有矩阵乘 ...
- 09、RNA降解图的计算过程
RNA降解是影响芯片质量的一个很重要的因素,因为RNA是从5’开始降解的,所以理论5’的荧光强度要低于3’.RNA降解曲线可以表现这种趋势. 以样品GSM286756.CEL和GSM286757.CE ...
- mpvue开发微信小程序之picker
微信使用picker组件,bingchange 换成@change即可使用监听函数和方法 此处注意与微信多了一个mp的信息才能获取到选中的值. 获取当前日期+时间 function formatTim ...
- 苹果浏览器和ios中,时间字符串转换问题
背景:在开发PC端项目和小程序时,遇到过一个时间字符串转化问题,在苹果浏览器和ios微信客户端里,"2018-10-15 18:20" 以 字符"-"拼接的时间 ...