mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-29 23:23:42 +08:00
规范格式
This commit is contained in:
42
leetcode/0695.Max-Area-of-Island/695. Max Area of Island.go
Normal file
42
leetcode/0695.Max-Area-of-Island/695. Max Area of Island.go
Normal file
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
var dir = [][]int{
|
||||
[]int{-1, 0},
|
||||
[]int{0, 1},
|
||||
[]int{1, 0},
|
||||
[]int{0, -1},
|
||||
}
|
||||
|
||||
func maxAreaOfIsland(grid [][]int) int {
|
||||
res := 0
|
||||
for i, row := range grid {
|
||||
for j, col := range row {
|
||||
if col == 0 {
|
||||
continue
|
||||
}
|
||||
area := areaOfIsland(grid, i, j)
|
||||
if area > res {
|
||||
res = area
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func isInGrid(grid [][]int, x, y int) bool {
|
||||
return x >= 0 && x < len(grid) && y >= 0 && y < len(grid[0])
|
||||
}
|
||||
|
||||
func areaOfIsland(grid [][]int, x, y int) int {
|
||||
if !isInGrid(grid, x, y) || grid[x][y] == 0 {
|
||||
return 0
|
||||
}
|
||||
grid[x][y] = 0
|
||||
total := 1
|
||||
for i := 0; i < 4; i++ {
|
||||
nx := x + dir[i][0]
|
||||
ny := y + dir[i][1]
|
||||
total += areaOfIsland(grid, nx, ny)
|
||||
}
|
||||
return total
|
||||
}
|
Reference in New Issue
Block a user