当前位置:网站首页>Number of islands
Number of islands
2022-04-23 09:11:00 【Boundless universe PY】
from collections import deque
# Parameters grid It's a 01 matrix
# Return value islands Is the number of islands
class Solution:
def numIslands(self, grid):
if not grid or not grid[0]:
return 0
islands = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j]:
self.bfs(grid, i, j)
islands += 1
return islands
def bfs(self, grid, x, y):
queue = deque([(x, y)])
grid[x][y] = False
while queue:
x, y = queue.popleft()
for delta_x, delta_y in [(1, 0), (0, -1), (-1, 0), (0, 1)]:
next_x = x + delta_x
next_y = y + delta_y
if not self.is_valid(grid, next_x, next_y):
continue
queue.append((next_x, next_y))
grid[next_x][next_y] = False
def is_valid(self, grid, x, y):
n, m = len(grid), len(grid[0])
return 0 <= x < n and 0 <= y < m and grid[x][y]
# The main function
if name == ‘main’:
generator= [
[1,1,1,1,2],
[0,1,0,0,1],
[0,0,0,1,1],
[0,0,0,0,0],
[0,0,0,0,1]
]
solution = Solution()
print(“ Input :”, generator)
print(“ Output :”, solution.numIslands(generator))

版权声明
本文为[Boundless universe PY]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230657123950.html
边栏推荐
- Brief steps to build a website / application using flash and H5
- L2-022 重排链表 (25 分)(map+结构体模拟)
- MYCAT configuration
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- Arbre de dépendance de l'emballage des ressources
- Go language self-study series | initialization of golang structure
- 错题汇总1
- How does kubernetes use harbor to pull private images
- Research purpose, construction goal, construction significance, technological innovation, technological effect
- ALV树(LL LR RL RR)插入删除
猜你喜欢

Multi view depth estimation by fusing single view depth probability with multi view geometry

L2-024 tribe (25 points) (and check the collection)

【SQL server速成之路】数据库的视图和游标

Valgrind and kcache grind use run analysis

数据清洗 ETL 工具Kettle的安装

Strength comparison vulnerability of PHP based on hash algorithm

Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise

MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)

Latex paper typesetting operation

Enterprise wechat application authorization / silent login
随机推荐
Share the office and improve the settled experience
node安装
Machine learning (VI) -- Bayesian classifier
About CIN, scanf and getline, getchar, CIN Mixed use of getline
Distributed message oriented middleware framework selection - Digital Architecture Design (7)
Illegal character in scheme name at index 0:
資源打包關系依賴樹
OpenCV中的图像处理 —— 轮廓入门+轮廓特征
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
Play with binary tree (25 points)
Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
Resource packaging dependency tree
Harbor enterprise image management system
错题汇总1
L2-022 rearrange linked list (25 points) (map + structure simulation)
2022-04-22 openebs cloud native storage
Output first order traversal according to second order and middle order traversal (25 points)
Enterprise wechat application authorization / silent login
数字政府建设中政务中台中的技术创新点