困难题全靠题解哈哈哈哈哈
用的单调栈 感谢题解
class Solution {
public int trap(int[] height) {
int ans = 0;
if(height.length < 2) return 0;
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < height.length; i++){
if(stack.isEmpty()){
stack.push(i);
}else if(height[i] <= height[stack.peek()]){
stack.push(i);
}else{
while(!stack.isEmpty() && height[i] > height[stack.peek()]){
int b = stack.peek();
stack.pop();
if(!stack.isEmpty()){
int h = Math.min(height[stack.peek()], height[i]) - height[b];
int w = i - stack.peek() - 1;
ans += h * w;
}
}
stack.push(i);
}
}
return ans;
}
}