Leetcode42.接雨水

class Solution {
public:
int trap(vector& heights) {
vector left, right;
int length = heights.size();
// left[0]=heights[0];
// right[length-1]=heights[length-1];
int water=0;
for(int i=1; i<length; ++i)
left[i] = max(left[i-1], heights[i]);
for(int i=length-2; i>=0; --i)
right[i] = max(right[i+1], heights[i]);
for(int i=1; i<length-1; ++i){
int temp = min(left[i], right[i]);
water += max(0, temp - heights[i]);
}
return water;
}
};

猜你喜欢

转载自blog.csdn.net/chenguiyuan1234/article/details/89399085
今日推荐