
原题链接
代码如下:
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
class Solution {
public int[] dailyTemperatures(int[] T) {
int len = T.length;
int[] ans = new int[len];
Deque<Integer> stack = new LinkedList<Integer>();
for (int i = 0; i < len; i++) {
int curTemper = T[i];
while (!stack.isEmpty() && curTemper > T[stack.peek()]) {
int preIndex = stack.pop();
ans[preIndex] = i - preIndex;
}
stack.push(i);
}
return ans;
}
public static void main(String[] args) {
Solution a = new Solution();
int[] k = { 73, 74, 75, 71, 69, 72, 76, 73 };
System.out.println(Arrays.toString(a.dailyTemperatures(k)));
}
}