添加 0042接雨水 python3版本 双指针法

添加 0042接雨水 python3版本 双指针法
This commit is contained in:
jojoo15
2021-07-23 23:13:36 +02:00
committed by GitHub
parent bf2a4581b2
commit 7b263b445d

View File

@ -354,4 +354,24 @@ public:
## 其他语言版本
python 版本
双指针法
```python3
class Solution:
def trap(self, height: List[int]) -> int:
res = 0
for i in range(len(height)):
if i == 0 or i == len(height)-1: continue
lHight = height[i-1]
rHight = height[i+1]
for j in range(i-1):
if height[j] > lHight:
lHight = height[j]
for k in range(i+2,len(height)):
if height[k] > rHight:
rHight = height[k]
res1 = min(lHight,rHight) - height[i]
if res1 > 0:
res += res1
return res
```