Files
Hot-100-Algorithm/TwoPointers/jieyushui/main.go
JACKYMYPERSON 0b67a0a33e 更新双指针
2025-08-16 12:26:01 +08:00

34 lines
691 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}