mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
@ -348,6 +348,99 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Go
|
||||
|
||||
```go
|
||||
func largestIsland(grid [][]int) int {
|
||||
dir := [][]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}}
|
||||
n := len(grid)
|
||||
m := len(grid[0])
|
||||
area := 0
|
||||
visited := make([][]bool, n)
|
||||
for i := 0; i < n; i++ {
|
||||
visited[i] = make([]bool, m)
|
||||
}
|
||||
gridNum := make(map[int]int, 0) // 记录每一个岛屿的面积
|
||||
mark := 2 // 记录每个岛屿的编号
|
||||
isAllGrid := true
|
||||
res := 0 // 标记是否整个地图都是陆地
|
||||
|
||||
var dfs func(grid [][]int, visited [][]bool, x, y, mark int)
|
||||
dfs = func(grid [][]int, visited [][]bool, x, y, mark int) {
|
||||
// 终止条件:访问过的节点 或者 遇到海水
|
||||
if visited[x][y] || grid[x][y] == 0 {
|
||||
return
|
||||
}
|
||||
visited[x][y] = true // 标记访问过
|
||||
grid[x][y] = mark // 给陆地标记新标签
|
||||
area++
|
||||
for i := 0; i < 4; i++ {
|
||||
nextX := x + dir[i][0]
|
||||
nextY := y + dir[i][1]
|
||||
if nextX < 0 || nextX >= len(grid) || nextY < 0 || nextY >= len(grid[0]) {
|
||||
continue
|
||||
}
|
||||
dfs(grid, visited, nextX, nextY, mark)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < m; j++ {
|
||||
if grid[i][j] == 0 {
|
||||
isAllGrid = false
|
||||
}
|
||||
if !visited[i][j] && grid[i][j] == 1 {
|
||||
area = 0
|
||||
dfs(grid, visited, i, j, mark) // 将与其链接的陆地都标记上 true
|
||||
gridNum[mark] = area // 记录每一个岛屿的面积
|
||||
mark++ // 更新下一个岛屿编号
|
||||
}
|
||||
}
|
||||
}
|
||||
if isAllGrid {
|
||||
return n * m
|
||||
}
|
||||
// 根据添加陆地的位置,计算周边岛屿面积之和
|
||||
visitedGrid := make(map[int]struct{}) // 标记访问过的岛屿
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < m; j++ {
|
||||
count := 1 // 记录连接之后的岛屿数量
|
||||
visitedGrid = make(map[int]struct{}) // 每次使用时,清空
|
||||
if grid[i][j] == 0 {
|
||||
for k := 0; k < 4; k++ {
|
||||
// 计算相邻坐标
|
||||
nearI := i + dir[k][0]
|
||||
nearJ := j + dir[k][1]
|
||||
if nearI < 0 || nearI >= len(grid) || nearJ < 0 || nearJ >= len(grid[0]) {
|
||||
continue
|
||||
}
|
||||
// 添加过的岛屿不要重复添加
|
||||
if _, ok := visitedGrid[grid[nearI][nearJ]]; ok {
|
||||
continue
|
||||
}
|
||||
// 把相邻四面的岛屿数量加起来
|
||||
count += gridNum[grid[nearI][nearJ]]
|
||||
// 标记该岛屿已经添加过
|
||||
visitedGrid[grid[nearI][nearJ]] = struct{}{}
|
||||
}
|
||||
}
|
||||
res = max827(res, count)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func max827(x, y int) int {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
### JavaScript
|
||||
|
||||
```JavaScript
|
||||
@ -411,8 +504,6 @@ return res;
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user