Skip to content

Commit 6a8a75e

Browse files
author
lucifer
committed
chore: 优化 python 代码
1 parent 98159d7 commit 6a8a75e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

problems/42.trapping-rain-water.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,16 @@ Python Code:
111111
class Solution:
112112
def trap(self, heights: List[int]) -> int:
113113
n = len(heights)
114-
l, r = [0] * (n + 1), [0] * (n + 1)
114+
l, r = [0] * n, [0] * n
115115
ans = 0
116-
for i in range(1, len(heights) + 1):
116+
for i in range(1, len(heights)):
117117
l[i] = max(l[i - 1], heights[i - 1])
118-
for i in range(len(heights) - 1, 0, -1):
119-
r[i] = max(r[i + 1], heights[i])
118+
for i in range(len(heights) - 2, 0, -1):
119+
r[i] = max(r[i + 1], heights[i+1])
120120
for i in range(len(heights)):
121-
ans += max(0, min(l[i + 1], r[i]) - heights[i])
121+
ans += max(0, min(l[i], r[i]) - heights[i])
122122
return ans
123+
123124
```
124125

125126
C++ Code:

0 commit comments

Comments
 (0)