题目来源leetcode42题
https://leetcode-cn.com/problems/trapping-rain-water/
解题思路:我没用到堆栈(感觉太麻烦),这个题目直接遍历整个数组就行了,代码挺简单的,应该能看懂哈。
class Solution {
public:
int trap(vector<int>& height) {
int len=height.size();
if(len<=0) return 0;
int current=0;
int res=0;
for(int i=0;i<len;i++)
{
if(height[i]<height[current])
{
res+=height[current]-height[i];
}
else
{
current=i;
}
}
int j=len-1;
if(current!=len-1)
{
for(int i=len-1;i!=current;i--)
{
res-=height[current]-height[i];
if(height[i]<height[j])
{
res+=height[j]-height[i];
}
else
{
j=i;
}
}
}
return res;
}
};