1 import java.util.Stack; 2 3 public class DailyTemperatures { 4 public int[] dailyTemperatures(int[] T) { 5 Stack<Integer> stack = new Stack<>(); 6 int[] res = new int[T.length]; 7 for(int i = T.length - 1; i >= 0; i--) { 8 if(stack.isEmpty()) { 9 stack.push(i); 10 res[i] = 0; 11 } The else { 12 is IF (T [I] < T [stack.peek ()]) { 13 is RES [I] = stack.peek () - I; 14 stack.push (I); 15 } the else { 16 . 17 / / a practically only through the bad for its retention of code 18 is the while (stack.isEmpty () && T [stack.peek ()] <=! T [I]) { . 19 stack.pop (); 20 is } 21 is IF (stack.isEmpty ()) { 22 is RES [I] = 0 ; 23 is }else { 24 res[i] = stack.peek() - i; 25 } 26 stack.push(i); 27 28 } 29 } 30 } 31 return res; 32 } 33 }
According to the list of daily temperatures, rebuild a list, enter the corresponding position is how long you need to wait until the temperature rises more than the number of days of that date. After all, if not increased, please use 0 instead of at the location.
For example, given a list of temperatures = [73, 74, 75, 71, 69, 72, 76, 73], you should be output [1, 1, 4, 2, 1, 1, 0, 0].
Tip: Temperature range list length is [1, 30000]. Are each Fahrenheit temperature values, are integers in the range [30, 100].
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/daily-temperatures