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

[pull] master from youngyangyang04:master #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Nov 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
添加0827.最大人工岛Python3版本
  • Loading branch information
gggxxxx committed Oct 31, 2023
commit c35a886d35eef5870ba2ab862b6c2da99e98bc31
65 changes: 65 additions & 0 deletions problems/0827.最大人工岛.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,71 @@ class Solution {
}
```

### Python

```python

class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
visited = set() #标记访问过的位置
m, n = len(grid), len(grid[0])
res = 0
island_size = 0 #用于保存当前岛屿的尺寸
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]] #四个方向
islands_size = defaultdict(int) #保存每个岛屿的尺寸

def dfs(island_num, r, c):
visited.add((r, c))
grid[r][c] = island_num #访问过的位置标记为岛屿编号
nonlocal island_size
island_size += 1
for i in range(4):
nextR = r + directions[i][0]
nextC = c + directions[i][1]
if (nextR not in range(m) or #行坐标越界
nextC not in range(n) or #列坐标越界
(nextR, nextC) in visited): #坐标已访问
continue
if grid[nextR][nextC] == 1: #遇到有效坐标,进入下一个层搜索
dfs(island_num, nextR, nextC)

island_num = 2 #初始岛屿编号设为2, 因为grid里的数据有0和1, 所以从2开始编号
all_land = True #标记是否整个地图都是陆地
for r in range(m):
for c in range(n):
if grid[r][c] == 0:
all_land = False #地图里不全是陆地
if (r, c) not in visited and grid[r][c] == 1:
island_size = 0 #遍历每个位置前重置岛屿尺寸为0
dfs(island_num, r, c)
islands_size[island_num] = island_size #保存当前岛屿尺寸
island_num += 1 #下一个岛屿编号加一
if all_land:
return m * n #如果全是陆地, 返回地图面积

count = 0 #某个位置0变成1后当前岛屿尺寸
#因为后续计算岛屿面积要往四个方向遍历,但某2个或3个方向的位置可能同属于一个岛,
#所以为避免重复累加,把已经访问过的岛屿编号加入到这个集合
visited_island = set() #保存访问过的岛屿
for r in range(m):
for c in range(n):
if grid[r][c] == 0:
count = 1 #把由0转换为1的位置计算到面积里
visited_island.clear() #遍历每个位置前清空集合
for i in range(4):
nearR = r + directions[i][0]
nearC = c + directions[i][1]
if nearR not in range(m) or nearC not in range(n): #周围位置越界
continue
if grid[nearR][nearC] in visited_island: #岛屿已访问
continue
count += islands_size[grid[nearR][nearC]] #累加连在一起的岛屿面积
visited_island.add(grid[nearR][nearC]) #标记当前岛屿已访问
res = max(res, count)
return res

```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down

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