修改目录名称
This commit is contained in:
33
双指针/接雨水/main.go
Normal file
33
双指针/接雨水/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user