当前位置:网站首页>【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
边栏推荐
- Recommended configuration of tidb software and hardware environment
- 高考录取分数线爬虫
- 损失函数与正负样本分配:YOLO系列
- 提前批院校名称
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- Deploy tidb cluster with tiup
- Bing. Com website
- 10_Redis_geospatial_命令
- . Net again! Happy 20th birthday
- LeetCode刷题——奇偶链表#328#Medium
猜你喜欢
Mavn builds nexus private server
Bing.com網站
21_ Redis_ Analysis of redis cache penetration and avalanche
15_ Redis_ Redis. Conf detailed explanation
Yolov5 code reproduction and server operation
Leetcode skimming -- sum of two integers 371 medium
10_Redis_geospatial_命令
Learn the method code example of converting timestamp to uppercase date using PHP
03.golang初步使用
Pytorch 保存tensor到.mat文件
随机推荐
Bing.com網站
Leetcode skimming - remove duplicate letters 316 medium
How to conduct TPC-C test on tidb
Force deduction solution summarizes the lucky numbers in 1380 matrix
Leetcode skimming -- incremental ternary subsequence 334 medium
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
搭建自己的语义分割平台deeplabV3+
Practical debugging skills
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
士官类学校名录
14_ Redis_ Optimistic lock
Redux - detailed explanation
How to avoid 7 common problems in mobile and network availability testing
LeetCode刷题——递增的三元子序列#334#Medium
07_ Hash
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
微信支付宝账户体系和支付接口业务流程
MySQL calculate n-day retention rate
LeetCode刷题——验证二叉树的前序序列化#331#Medium