-
Notifications
You must be signed in to change notification settings - Fork 1
/
surface-area.go
58 lines (45 loc) · 918 Bytes
/
surface-area.go
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
49
50
51
52
53
54
55
56
57
58
package myMath
// 892
func surfaceArea(grid [][]int) int {
total := 0
col := len(grid)
for i := 0; i < col; i++ {
row := len(grid[i])
// 第一列,或者列中有间隔0
if grid[i][0] > 0 {
if i == 0 || grid[i-1][0] == 0 {
total += count(grid[i][0])
} else {
total += count(grid[i][0]) - min( grid[i][0], grid[i-1][0]) * 2
}
}
for j := 1; j < row; j++ {
if grid[i][j] > 0 {
total += count(grid[i][j])
// 周边都有的方块判断
if i > 0 {
if grid[i][j-1] > 0 {
total -= min(grid[i][j-1], grid[i][j]) * 2
}
if grid[i-1][j] > 0 {
total -= min(grid[i-1][j], grid[i][j]) * 2
}
} else {
if grid[i][j-1] > 0 {
total -= min(grid[i][j-1], grid[i][j]) * 2
}
}
}
}
}
return total
}
func count(a int) int {
return (a * 6) - (a-1)*2
}
func min (a, b int) int {
if a >= b {
return b
}
return a
}