当前位置:网站首页>【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
边栏推荐
- 16_Redis_Redis持久化
- 19_Redis_宕机后手动配置主机
- 搭建自己的语义分割平台deeplabV3+
- 高考分数线爬取
- 14_ Redis_ Optimistic lock
- LeetCode刷题——递增的三元子序列#334#Medium
- FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- 15_Redis_Redis.conf详解
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
猜你喜欢

16_Redis_Redis持久化

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

03_线性表_链表

自定义异常

21_ Redis_ Analysis of redis cache penetration and avalanche

Leetcode question brushing - parity linked list 328 medium

Oracle primary key auto increment

Leetcode skimming -- count the number of numbers with different numbers 357 medium

党史纪实主题公益数字文创产品正式上线

19_ Redis_ Manually configure the host after downtime
随机推荐
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
Infra11199 database system
03. Preliminary use of golang
【LeetCode】1140-石子游戏II
Data analysis thinking analysis methods and business knowledge - business indicators
Bing.com网站
Tidb environment and system configuration check
. Solution to the problem of Chinese garbled code when net core reads files
LeetCode_ String_ Simple_ 412.Fizz Buzz
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
20_Redis_哨兵模式
19_Redis_宕机后手动配置主机
16_ Redis_ Redis persistence
21_Redis_浅析Redis缓存穿透和雪崩
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
QML pop-up frame, customizable
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
语义分割学习笔记(一)
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
18_Redis_Redis主从复制&&集群搭建