Skip to content

Commit ec05c96

Browse files
Add files via upload
1 parent 93eef71 commit ec05c96

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 根据答案给出的双指针法
2+
# 48ms 82.75%
3+
class Solution:
4+
def trap(self, height):
5+
"""
6+
:type height: List[int]
7+
:rtype: int
8+
"""
9+
left, right = 0, len(height) - 1
10+
left_max = right_max = 0
11+
area = 0
12+
while left < right:
13+
if height[left] <= height[right]:
14+
left_max = max(height[left], left_max)
15+
area += left_max - height[left]
16+
left += 1
17+
else:
18+
right_max = max(height[right], right_max)
19+
area += right_max - height[right]
20+
right -= 1
21+
return area

0 commit comments

Comments
 (0)