方法一:集合法
var trap = function(height) {
const stack=[]
let i=0,ans=0
while(i<height.length){
while(stack.length!==0&&height[i]>height[stack[stack.length-1]]){
let top=height.pop()
if(stack.length===0)break;
let distance=i-stack[stack.length-1]-1
let height2=Math.min(height[i],height[stack[stack.length-1]]-height[top])
console.log(distance,height2);
ans+=distance * height2
}
stack.push(i++)
}
return ans
};
方法二:栈