当前位置:网站首页>[leetcode] 200 number of islands
[leetcode] 200 number of islands
2022-07-02 15:36:00 【Crisp ~】
Here you are ‘1’( land ) and ‘0’( water ) A two-dimensional mesh of , Please calculate the number of islands in the grid .
Islands are always surrounded by water , And each island can only be combined by horizontal direction / Or a vertical connection between adjacent lands .
Besides , You can assume that all four sides of the mesh are surrounded by water .
Example 1:
Input :
grid = [
[“1”,“1”,“1”,“1”,“0”],
[“1”,“1”,“0”,“1”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“0”,“0”,“0”]
]
Output : 1
Example 2:
Input : grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
Output : 3
Tips :
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 300
- grid[i][j] The value of is ‘0’ or ‘1’
notes :
In case of 1 Immediately set as 0, Don't wait from the queue or Take it out of the stack and set it to 0, It will cause a large number of coordinate points to be repeated into the team or Push , Make the time exceed the limit !!!
## Breadth first traversal
class Solution(object):
def numIslands(self, grid):
n = len(grid)# Get array length
if n == 0:
return 0
m = len(grid[0])# Get array width
cnt = 0# Number of Islands
queue = []# Queue container
for i in range(n):
for j in range(m):
if grid[i][j] == "1":# If you meet land, start traversing
cnt+=1# The number of islands plus one
grid[i][j]="0"# Set as 0, Avoid repeated visits to
queue.append((i,j))# Join the queue , To traverse the
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:# Traverse in four directions
if 0<=xx<n and 0<=yy<m and grid[xx][yy]=="1":
grid[xx][yy]="0"
queue.append((xx,yy))
return cnt
## Depth-first traversal
class Solution(object):
def numIslands(self, grid):
n = len(grid)
if n == 0:
return 0
m = len(grid[0])
cnt = 0
stack = []
for i in range(n):
for j in range(m):
if grid[i][j] == "1":
cnt+=1
grid[i][j]="0"
stack.insert(0,(i,j))
while stack:
x,y = stack[0]
stack.pop(0)
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if 0<=xx<n and 0<=yy<m and grid[xx][yy]=="1":
grid[xx][yy]="0"
stack.insert(0,(xx,yy))
return cnt
边栏推荐
- 13_Redis_事务
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- Loss function and positive and negative sample allocation: Yolo series
- Pytoch saves tensor to Mat file
- PTA 天梯赛习题集 L2-001 城市间紧急救援
- Redux - detailed explanation
- Kibana basic operation
- folium,确诊和密接轨迹上图
- Bing.com網站
- folium地图无法显示的问题,临时性解决方案如下
猜你喜欢

损失函数与正负样本分配:YOLO系列

SQL transaction

Pytorch 保存tensor到.mat文件

Mavn builds nexus private server

03. Preliminary use of golang

YOLOV5 代码复现以及搭载服务器运行

18_ Redis_ Redis master-slave replication & cluster building

How to find a sense of career direction

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

彻底弄懂浏览器强缓存和协商缓存
随机推荐
Data analysis thinking analysis methods and business knowledge - business indicators
03_线性表_链表
Bing. Site Internet
你不知道的Set集合
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
16_ Redis_ Redis persistence
20_ Redis_ Sentinel mode
List set & UML diagram
Bing.com網站
Mavn builds nexus private server
folium地图无法显示的问题,临时性解决方案如下
13_Redis_事务
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
微信支付宝账户体系和支付接口业务流程
Markdown tutorial
LeetCode刷题——验证二叉树的前序序列化#331#Medium
Bing.com网站