Generate a new list based on the daily temperature list. The output of the corresponding position is: the minimum number of days to wait for higher temperatures to be observed. If the temperature does not rise after that point, substitute 0 at that point.
For example, given a list of temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output would be [1, 1, 4, 2, 1, 1, 0, 0].
Note: The length range of the temperature list is [1, 30000]. Each temperature is an integer in the [30, 100] range of degrees Fahrenheit.
In the discussion boards, it’s easy to use a monotonic stack, but a two-tier for loop is easier.
public int[] dailyTemperatures(int[] nums) { int[] res = new int[nums.length]; for(int i = 0; i < nums.length-1; i++){ int a = 0; for(int j = i+1; j < nums.length; j++){ if(nums[j] > nums[i]){ res[i] = j-i; a=1; break; }else { a = 0; } } if(a == 0){ res[i] = 0; } } res[res.length-1] = 0; return res; }}Copy the code