当前位置:网站首页>【LeetCode】695-岛屿的最大面积
【LeetCode】695-岛屿的最大面积
2022-07-02 12:09:00 【酥酥~】
给你一个大小为 m x n 的二进制矩阵 grid 。
岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。
岛屿的面积是岛上值为 1 的单元格的数目。
计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。
示例 1:
输入:
grid = [
[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]]
输出: 6
解释: 答案不应该是 11 ,因为岛屿只能包含水平或垂直这四个方向上的 1 。
示例 2:
输入: grid = [[0,0,0,0,0,0,0,0]]
输出: 0
提示:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 50
- grid[i][j] 为 0 或 1
#广度优先搜索
class Solution(object):
def maxAreaOfIsland(self, grid):
n = len(grid)#数列长度
m = len(grid[0])#数列宽度
max_S = 0#记录最大面积
queue = []#队列
for i in range(n):
for j in range(m):
if grid[i][j]==1:#遇见陆地就开始搜索与之相邻的所有陆地
S=1
grid[i][j]=0
queue.append((i,j))
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<n and 0<=yy<m and grid[xx][yy]==1:
S+=1
grid[xx][yy]=0
queue.append((xx,yy))
max_S=max(max_S,S)
return max_S
class Solution(object):
def maxAreaOfIsland(self, grid):
n = len(grid)
m = len(grid[0])
max_S = 0
stack = []
for i in range(n):
for j in range(m):
if grid[i][j]==1:
S=1
grid[i][j]=0
stack.append((i,j))
while stack:
x,y = stack[-1]
stack.pop()
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<n and 0<=yy<m and grid[xx][yy]==1:
S+=1
grid[xx][yy]=0
stack.append((xx,yy))
max_S=max(max_S,S)
return max_S
边栏推荐
- Oracle primary key auto increment
- Engineer evaluation | rk3568 development board hands-on test
- Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
- Tidb environment and system configuration check
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- 语义分割学习笔记(一)
- folium,确诊和密接轨迹上图
- Tidb data migration tool overview
- MD5 encryption
- 05_ queue
猜你喜欢

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

Oracle primary key auto increment

16_Redis_Redis持久化

14_Redis_乐观锁

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

Markdown tutorial

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?

Yolov5 code reproduction and server operation

03_ Linear table_ Linked list
随机推荐
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Infra11199 database system
16_ Redis_ Redis persistence
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
JVM architecture, classloader, parental delegation mechanism
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
Application of CDN in game field
Solution of Queen n problem
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
高考分数线爬取
19_Redis_宕机后手动配置主机
Force deduction solution summary 2029 stone game IX
10_Redis_geospatial_命令
PHP method to get the index value of the array item with the largest key value in the array
工程师评测 | RK3568开发板上手测试
Practical debugging skills
14_ Redis_ Optimistic lock
How to intercept the value of a key from the JSON string returned by wechat?
LeetCode_ String_ Simple_ 412.Fizz Buzz