From 54e317de651b2a4b4934caa310a7ea8118efa3ef Mon Sep 17 00:00:00 2001 From: Jojo Date: Wed, 8 Jan 2020 15:02:10 -0800 Subject: [PATCH 1/2] Update 42.trapping-rain-water.md Add python code --- problems/42.trapping-rain-water.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/42.trapping-rain-water.md b/problems/42.trapping-rain-water.md index 5674ea35c..ca21f3019 100644 --- a/problems/42.trapping-rain-water.md +++ b/problems/42.trapping-rain-water.md @@ -88,3 +88,28 @@ var trap = function(height) { }; ``` + +```python + +class Solution: + def trap(self, height: List[int]) -> int: + maxLeft, maxRight, volum = 0, 0, 0 + maxLeftStack, maxRightStack = [], [] + for h in height: + if h > maxLeft: + maxLeftStack.append(h) + maxLeft = h + else: + maxLeftStack.append(maxLeft) + for h in height[::-1]: + if h > maxRight: + maxRightStack.append(h) + maxRight = h + else: + maxRightStack.append(maxRight) + maxRightStack = maxRightStack[::-1] + for i in range(1, len(height) - 1): + minSide = min(maxLeftStack[i], maxRightStack[i]) + volum += minSide - height[i] + return volum +``` From 7b0c8fff22ad7ceaa450d3303d8e849c285121b6 Mon Sep 17 00:00:00 2001 From: lucifer Date: Thu, 9 Jan 2020 10:26:18 +0800 Subject: [PATCH 2/2] Update 42.trapping-rain-water.md --- problems/42.trapping-rain-water.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/42.trapping-rain-water.md b/problems/42.trapping-rain-water.md index ca21f3019..f0404bf31 100644 --- a/problems/42.trapping-rain-water.md +++ b/problems/42.trapping-rain-water.md @@ -52,6 +52,10 @@ for(let i = 0; i < height.length; i++) { ## 代码 +代码支持 JavaScript,Python3: + +JavaScript Code: + ```js /* @@ -89,6 +93,8 @@ var trap = function(height) { ``` +Python Code: + ```python class Solution: