Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e58860

Browse files
Merge pull request youngyangyang04#2325 from gggxxxx/XiongGu-branch
添加0827.最大人工岛Python3版本
2 parents 83efa79 + d1fc90f commit 2e58860

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

‎problems/0827.最大人工岛.md‎

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -284,64 +284,68 @@ class Solution {
284284

285285
### Python
286286

287-
```Python
288-
class Solution(object):
289-
# 可能的移动方向
290-
DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
291-
292-
def exploreIsland(self, row, col, grid, visited, island_id):
293-
"""
294-
从给定的单元格开始,使用深度优先搜索探索岛屿。
295-
"""
296-
if (row < 0 or row >= len(grid) or col < 0 or col >= len(grid[0]) or
297-
visited[row][col] or grid[row][col] == 0):
298-
return 0
299-
300-
visited[row][col] = True
301-
grid[row][col] = island_id
302-
island_size = 1
303-
for dr, dc in self.DIRECTIONS:
304-
island_size += self.exploreIsland(row + dr, col + dc, grid, visited, island_id)
305-
return island_size
306-
307-
def largestIsland(self, grid):
308-
"""
309-
通过最多将一个0更改为1,找到可以形成的最大岛屿的大小。
310-
"""
311-
rows, cols = len(grid), len(grid[0])
312-
island_sizes = {}
313-
island_id = 2 # 从2开始标记岛屿(因为0代表水,1代表未被发现的陆地)
314-
is_all_land = True
315-
visited = [[False] * cols for _ in range(rows)]
316-
317-
# 标记每个岛屿并存储其大小
318-
for r in range(rows):
319-
for c in range(cols):
287+
```python
288+
289+
class Solution:
290+
def largestIsland(self, grid: List[List[int]]) -> int:
291+
visited = set() #标记访问过的位置
292+
m, n = len(grid), len(grid[0])
293+
res = 0
294+
island_size = 0 #用于保存当前岛屿的尺寸
295+
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]] #四个方向
296+
islands_size = defaultdict(int) #保存每个岛屿的尺寸
297+
298+
def dfs(island_num, r, c):
299+
visited.add((r, c))
300+
grid[r][c] = island_num #访问过的位置标记为岛屿编号
301+
nonlocal island_size
302+
island_size += 1
303+
for i in range(4):
304+
nextR = r + directions[i][0]
305+
nextC = c + directions[i][1]
306+
if (nextR not in range(m) or #行坐标越界
307+
nextC not in range(n) or #列坐标越界
308+
(nextR, nextC) in visited): #坐标已访问
309+
continue
310+
if grid[nextR][nextC] == 1: #遇到有效坐标,进入下一个层搜索
311+
dfs(island_num, nextR, nextC)
312+
313+
island_num = 2 #初始岛屿编号设为2, 因为grid里的数据有0和1, 所以从2开始编号
314+
all_land = True #标记是否整个地图都是陆地
315+
for r in range(m):
316+
for c in range(n):
320317
if grid[r][c] == 0:
321-
is_all_land = False
322-
elif not visited[r][c] and grid[r][c] == 1:
323-
island_size = self.exploreIsland(r, c, grid, visited, island_id)
324-
island_sizes[island_id] = island_size
325-
island_id += 1
326-
327-
# 如果整个网格是陆地,则返回其大小
328-
if is_all_land:
329-
return rows * cols
330-
331-
# 计算可以通过将一个0更改为1来形成的最大岛屿
332-
max_island_size = 0
333-
for r in range(rows):
334-
for c in range(cols):
318+
all_land = False #地图里不全是陆地
319+
if (r, c) not in visited and grid[r][c] == 1:
320+
island_size = 0 #遍历每个位置前重置岛屿尺寸为0
321+
dfs(island_num, r, c)
322+
islands_size[island_num] = island_size #保存当前岛屿尺寸
323+
island_num += 1 #下一个岛屿编号加一
324+
if all_land:
325+
return m * n #如果全是陆地, 返回地图面积
326+
327+
count = 0 #某个位置0变成1后当前岛屿尺寸
328+
#因为后续计算岛屿面积要往四个方向遍历,但某2个或3个方向的位置可能同属于一个岛,
329+
#所以为避免重复累加,把已经访问过的岛屿编号加入到这个集合
330+
visited_island = set() #保存访问过的岛屿
331+
for r in range(m):
332+
for c in range(n):
335333
if grid[r][c] == 0:
336-
adjacent_islands = set()
337-
for dr, dc in self.DIRECTIONS:
338-
nr, nc = r + dr, c + dc
339-
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] > 1:
340-
adjacent_islands.add(grid[nr][nc])
341-
new_island_size = sum(island_sizes[island] for island in adjacent_islands) + 1
342-
max_island_size = max(max_island_size, new_island_size)
343-
344-
return max_island_size
334+
count = 1 #把由0转换为1的位置计算到面积里
335+
visited_island.clear() #遍历每个位置前清空集合
336+
for i in range(4):
337+
nearR = r + directions[i][0]
338+
nearC = c + directions[i][1]
339+
if nearR not in range(m) or nearC not in range(n): #周围位置越界
340+
continue
341+
if grid[nearR][nearC] in visited_island: #岛屿已访问
342+
continue
343+
count += islands_size[grid[nearR][nearC]] #累加连在一起的岛屿面积
344+
visited_island.add(grid[nearR][nearC]) #标记当前岛屿已访问
345+
res = max(res, count)
346+
return res
347+
348+
345349
```
346350

347351
<p align="center">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /