Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9401c4d

Browse files
Merge pull request youngyangyang04#842 from Axehco/master
添加 0042接雨水 Go语言 版本
2 parents 5234228 + 536b359 commit 9401c4d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0042.接雨水.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,31 @@ class Solution:
579579

580580
Go:
581581

582+
```go
583+
func trap(height []int) int {
584+
var left, right, leftMax, rightMax, res int
585+
right = len(height) - 1
586+
for left < right {
587+
if height[left] < height[right] {
588+
if height[left] >= leftMax {
589+
leftMax = height[left] // 设置左边最高柱子
590+
} else {
591+
res += leftMax - height[left] // //右边必定有柱子挡水,所以遇到所有值小于等于leftMax的,全部加入水池中
592+
}
593+
left++
594+
} else {
595+
if height[right] > rightMax {
596+
rightMax = height[right] // //设置右边最高柱子
597+
} else {
598+
res += rightMax - height[right] // //左边必定有柱子挡水,所以,遇到所有值小于等于rightMax的,全部加入水池
599+
}
600+
right--
601+
}
602+
}
603+
return res
604+
}
605+
```
606+
582607
JavaScript:
583608
```javascript
584609
//双指针

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /