根据往后 n 天的天气预报,计算每一天需要等待几天才会出现一次更高的气温,如果往后都没有更高的气温,则用 0 补位。
例如往后三天的气温是 [1,2,3] , 则输出 [1,1,0]
数据范围:
,每天的温度会满足 ![0 \le dailyTemperatures[i] \le 1000 \](https://www.nowcoder.com/equation?tex=0%20%5Cle%20dailyTemperatures%5Bi%5D%20%5Cle%201000%20%20%5C)
[1,2,3]
[1,1,0]
[2,4,5,9,10,0,9]
[1,1,1,1,0,1,0]
[3,1,4]
[2,1,0]
import java.util.*;
/**
* NC208 每日温度
* @author d3y1
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 每日温度
* @param dailyTemperatures int整型一维数组
* @return int整型一维数组
*/
public int[] temperatures (int[] dailyTemperatures) {
int n = dailyTemperatures.length;
int[] results = new int[n];
Stack<Integer> stack = new Stack<>();
// Deque<Integer> stack = new ArrayDeque<>();
// Deque<Integer> stack = new LinkedList<>();
for(int i=n-1; i>=0; i--){
// 单调栈 单调减(从右向左遍历)
while(!stack.isEmpty() && dailyTemperatures[stack.peek()]<=dailyTemperatures[i]){
stack.pop();
}
// if(stack.isEmpty()){
// results[i] = 0;
// }else{
// results[i] = stack.peek()-i;
// }
results[i] = stack.isEmpty() ? 0 : stack.peek()-i;
stack.push(i);
}
return results;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 每日温度
* @param dailyTemperatures int整型一维数组
* @return int整型一维数组
*/
public int[] temperatures (int[] dailyTemperatures) {
// write code here
int n = dailyTemperatures.length;
int[] result = new int[n];
Arrays.fill(result, 0);
int index = 0;
for(int i = 0; i < n; i++){
index = i + 1;
while(index < n){
if(dailyTemperatures[index] > dailyTemperatures[i]){
result[i] = index - i;
break;
}
index++;
}
}
return result;
}
} public int[] temperatures (int[] dailyTemperatures) {
int n = dailyTemperatures.length;
int[] res = new int[n];
//最后一个直接为0
res[n - 1] = 0;
//从倒数第2个开始向前遍历
for (int i = n - 2; i >= 0; i--) {
//从i的后一位开始比较
int j = i + 1;
while (j < n) {
//如果后一个比当前元素大,则记录距离
if (dailyTemperatures[i] < dailyTemperatures[j]) {
res[i] = j - i;
break;
} else if (res[j] == 0) {//后一位置为0,则说明后面没有比j位置更大温度,则当前遍历的i位置也为0
res[i] = 0;
break;
} else {//j位置不为0时,则直接下一个位置:为第一个比j位置大的数
j += res[j];
}
}
}
return res;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param temperatures int整型一维数组
* @return int整型一维数组
*/
public int[] temperatures (int[] temperatures) {
// write code here
Stack<Integer> stack = new Stack<>();
int[] res = new int[temperatures.length];
for(int i = 0; i < temperatures.length; i++){
if(stack.isEmpty() || temperatures[stack.peek()] > temperatures[i]){
stack.push(i);
}else{
if(temperatures[stack.peek()] == temperatures[i]){
stack.push(i);
}else{
while(!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]){
int cur = stack.pop();
res[cur] = i - cur;
}
stack.push(i);
}
}
}
// 因为res的初始值本来就是0,此步骤可以不要,但为了流程完整性,还是写上
while(!stack.isEmpty()){
res[stack.pop()] = 0;
}
return res;
}
} 宏观上看,遍历一遍数组的时间复杂度是O(n)。而弹出栈的元素绝不会再被压回去,所有的元素在整个算法流程中都会来一遍入栈和出栈,因此整体时间复杂度就是O(n)。开辟了一个栈空间来存放数组的索引,因此空间复杂度也为O(n)。 public int[] temperatures (int[] temperatures) {
// write code here
int[] ans = new int[temperatures.length];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < temperatures.length; i++){
while(!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i] ){
int temp = stack.pop();
ans[temp] = i-temp;
}
stack.push(i);
}
return ans;
}