public class Solution {
public int trap(int[] height) {
if (height == null || height.length < 3) {
return 0;
}
int start = 0, end = height.length - 1;
int area = 0, smaller = 0;
while (start < end) {
if (height[start] < height[end]) {
smaller = height[start];
while (start < end && height[start] <= smaller) {
area = area + smaller - height[start];
start++;
}
} else {
smaller = height[end];
while (start < end && height[end] <= smaller) {
area = area + smaller - height[end];
end--;
}
}
}
return area;
}
}
Trapping Rain Water
最新推荐文章于 2024-09-17 16:23:14 发布