题目:
思路:
https://www.bilibili.com/video/BV1PJ411H7P7/?spm_id_from=333.337.search-card.all.click&vd_source=2f682a60feabf0f730ad09e0f980ce83
单调栈
思考:
解决栈类问题,思考入栈,出栈条件;
思考栈内存储元素,是下标还是数据。
代码:
public int[] dailyTempratures(int[] temperatures) {int[] result = new int[temperatures.length];Arrays.fill(result, 0); // 注意这里要填0Deque<Integer> deque = new ArrayDeque<>();for (int i = 0; i < temperatures.length; i++) {while (!deque.isEmpty() && temperatures[i] > temperatures[deque.peek()]) {int index = deque.pop();result[index] = i-index; // 这里利用下标差值}deque.push(i);}return result;
}