Solution1: 栈
从左向右遍历,维护一个从栈底向栈顶递减的栈,遇到比栈顶小的入栈,遇到大的则出栈
写的很垃圾,AC 了之后就不想改了
//栈:解法1
class Solution {
public:
int trap(vector<int>& height) {
int res = 0;
stack<pair<int,int> > ht;
ht.push(pair<int,int>(0,-1));
for(int i=0;i<height.size();++i){
if(height[i] < (ht.top().first))
ht.push(pair<int,int>(height[i],i));
else{
while(ht.size()>1 && height[i] >= ht.top().first){
int bottom = ht.top().first;
if(height[i] == ht.top().first){
ht.pop();
break;
}
ht.pop();