int trap(vector<int>& height) {
int res = 0;
int len = (int)height.size();
if (len <= 1) {
return res;
}
int l = 0;
int r = len - 1;
int l_height = 0;
int r_height = 0;
while (l < r) {
if (height[l] < height[r]) {
l_height = max(l_height, height[l]);
res += l_height - height[l];
l++;
} else {
r_height = max(r_height, height[r]);
res += r_height - height[r];
r--;
}
}
return res;
}
标签:int,res,len,height,雨水,LeetCode42
From: https://www.cnblogs.com/txtp/p/16731431.html