901股票价格跨度 Posted on 2020-04-28 Words count in article: 183 | Reading time ≈ 1 901股票价格跨度 下面是代码123456789101112131415class 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了 123456789101112131415161718192021class 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; }};//真不知道为啥!!!! -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/28/901%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E8%B7%A8%E5%BA%A6/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.