Files
Hot-100-Algorithm/TwoPointers/jieyushui/main.go
JACKYMYPERSON a647e54009 更新双指针
2025-08-16 12:48:15 +08:00

34 lines
690 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], "当前左指针:", 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
}