05 August 2008
时空复杂度均为O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int[] dailyTemperatures(int[] T) {
int len = T.length;
int[] result = new int[len];
Deque<Integer> stack = new LinkedList<>();
// Stack<Integer> stack = new Stack<>(); // 一样
for (int i = 0; i < len; i++) {
//注意是while,一直check栈内元素
while (!stack.isEmpty() && T[i] > T[stack.peek()]) {
int preIndex = stack.pop();
result[preIndex] = i - preIndex; // stack里面存index
}
stack.push(i);
}
return result;
}
}