(双指针) $O(n)$
blablabla
时间复杂度
参考文献
C++ 代码
class Solution {
public:
int trap(vector<int>& height) {
int len = height.size();
if(len <= 1)
return 0;
int ret = 0;
int left = 0, right = len - 1;
int minval = 0;
while(left < right)
{
minval = min(height[left], height[right]);
while(left < right && height[left] <= minval)
{
ret += minval - height[left];
++left;
}
while(left < right && height[right] <= minval)
{
ret += minval - height[right];
--right;
}
}
return ret;
}
};