Skip to content

Commit e4f0d1d

Browse files
42. Trapping Rain Water (java)
1 parent 5a15b5f commit e4f0d1d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int trap(int[] height) {
3+
if (height == null || height.length == 0) return 0;
4+
int lx = 0, rx = height.length - 1, l = height[lx], r = height[rx], re = 0;
5+
while (lx < rx) {
6+
if (l < r) {
7+
lx++;
8+
if (height[lx] < l) re += l - height[lx];
9+
else l = height[lx];
10+
} else {
11+
rx--;
12+
if (height[rx] < r) re += r - height[rx];
13+
else r = height[rx];
14+
}
15+
}
16+
return re;
17+
}
18+
}

0 commit comments

Comments
 (0)