901-股票价格跨度

901-股票价格跨度

力扣901

https://leetcode-cn.com/problems/online-stock-span/

单调栈题目,我也不清楚力扣的输入样例是什么,但是直接return 1的话可以测试一下
看到已经返回了null, 1, 1…因此可以判断只需要完成这个类的next方法即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class StockSpanner {
private Deque<int[]> stack = new LinkedList<>(); //递减栈
public StockSpanner() {
}

public int next(int price) {
int num = 1;
int[] node = new int[2];
while (!stack.isEmpty() && price >= stack.peekLast()[0]){
int lastNum = stack.pollLast()[1];
num += lastNum;
}
node[0] = price;
node[1] = num;
stack.offerLast(node);
return num;
}
}

其他单调栈系列题可以点这里

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×