【LeetCode】【HOT】739. 每日温度
package hot;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class Solution739 {
public static void main(String[] args) {
int[] temperatures = {73,74,75,71,69,72,76,73};
Solution739 solution = new Solution739();
System.out.println(Arrays.toString(solution.method(temperatures)));
}
private int[] method(int[] temperatures){
int length = temperatures.length;
Deque<Integer> stack = new ArrayDeque<>();
int[] res = new int[length];
for(int i = 0; i < length; i++){
int temperature = temperatures[i];
while(!stack.isEmpty() && temperature > temperatures[stack.peek()]){
int preIndex = stack.pop();
res[preIndex] = i - preIndex;
}
stack.push(i);
}
return res;
}
}