34 lines
691 B
Go
34 lines
691 B
Go
package main
|
||
|
||
import "fmt"
|
||
|
||
func main() {
|
||
resule := trap([]int{4, 2, 0, 3, 2, 5})
|
||
fmt.Println("最大接水数为:", resule)
|
||
}
|
||
|
||
func trap(height []int) int {
|
||
leftP := 0
|
||
rightP := 1
|
||
result := 0
|
||
midres := 0
|
||
for {
|
||
if leftP > len(height)-1 || rightP > len(height)-1 {
|
||
break
|
||
}
|
||
if height[leftP] > height[rightP] {
|
||
midres += height[leftP] - height[rightP]
|
||
fmt.Println("result加上:", height[leftP]-height[rightP], "当前1左指针:", leftP, "当前右指针:", rightP)
|
||
rightP++
|
||
} else if height[leftP] <= height[rightP] && midres != 0 {
|
||
result += midres
|
||
leftP = rightP
|
||
rightP = leftP + 1
|
||
} else {
|
||
leftP++
|
||
rightP = leftP + 1
|
||
}
|
||
}
|
||
return result
|
||
}
|