Merge pull request #516 from jojoo15/patch-40

添加 0042接雨水 python3版本 双指针法
This commit is contained in:
程序员Carl
2021-07-26 09:45:34 +08:00
committed by GitHub

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
```