-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution463.go
More file actions
48 lines (39 loc) · 994 Bytes
/
solution463.go
File metadata and controls
48 lines (39 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package solution463
// ============================================================================
// 463. Island Perimeter
// URL: https://leetcode.com/problems/island-perimeter/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/463---Island-Perimeter
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_islandPerimeter
Benchmark_islandPerimeter-24 56296902 39.46 ns/op 0 B/op 0 allocs/op
PASS
*/
func islandPerimeter(grid [][]int) int {
var perimeter int
for y := 0; y < len(grid); y++ {
for x := 0; x < len(grid[y]); x++ {
if grid[y][x] == 0 {
continue
}
sides := 4
if y > 0 && grid[y-1][x] == 1 {
sides--
}
if x > 0 && grid[y][x-1] == 1 {
sides--
}
if y < len(grid)-1 && grid[y+1][x] == 1 {
sides--
}
if x < len(grid[y])-1 && grid[y][x+1] == 1 {
sides--
}
perimeter += sides
}
}
return perimeter
}