901股票价格跨度

901股票价格跨度

下面是代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class StockSpanner {
public:
StockSpanner() {
}

int next(int price) {

};


/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner* obj = new StockSpanner();
* int param_1 = obj->next(price);
*/

这题思路很简单啊,凭什么老是超时!?????

用pair存储,我一下子就想到了,但是老是超时,有时候AC了,有时候WA了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class StockSpanner {
private:
stack<pair<int, int>> s ;
public:
StockSpanner() {
}

int next(int price) {
//初始化1
int temp = 1;
//如果栈非空,那么和栈顶比,比栈顶的price大,那么就把他吸收进来,再pop掉
while (!s.empty() && price >= s.top().second) {
temp += s.top().first;
s.pop();
}
//然后入栈
s.push(make_pair(temp, price));
return temp;
}
};
//真不知道为啥!!!!
-------------本文结束,感谢您的阅读-------------