当前位置:网站首页>【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
边栏推荐
- 08_ strand
- Custom exception
- 06_ Stack and queue conversion
- Engineer evaluation | rk3568 development board hands-on test
- . Solution to the problem of Chinese garbled code when net core reads files
- 党史纪实主题公益数字文创产品正式上线
- Tidb cross data center deployment topology
- 15_Redis_Redis.conf详解
- Redux——详解
- 19_ Redis_ Manually configure the host after downtime
猜你喜欢

Engineer evaluation | rk3568 development board hands-on test

Yolov5 code reproduction and server operation

21_ Redis_ Analysis of redis cache penetration and avalanche

21_Redis_浅析Redis缓存穿透和雪崩

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568

16_Redis_Redis持久化

语义分割学习笔记(一)

Leetcode skimming -- sum of two integers 371 medium

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
随机推荐
高考录取分数线爬虫
04.进入云原生后的企业级应用构建的一些思考
Leetcode skimming -- count the number of numbers with different numbers 357 medium
Solve the problem of frequent interruption of mobaxterm remote connection
密码学基础知识
03_線性錶_鏈錶
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
LeetCode刷题——去除重复字母#316#Medium
Markdown tutorial
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
10_Redis_geospatial_命令
【LeetCode】1140-石子游戏II
工程师评测 | RK3568开发板上手测试
4. Jctree related knowledge learning
【网络安全】网络资产收集
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
17_Redis_Redis发布订阅
Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
10_ Redis_ geospatial_ command