栈
public int[] dailyTemperatures(int[] T) {
Deque<Integer> stack=new ArrayDeque<>();
int[] results=new int[T.length];
for (int i=0;i<T.length;i++){
//若当日温度大于栈顶温度
//出栈并且记录其与当日的天数差,存放于结果集中
while (!stack.isEmpty()&&T[i]>T[stack.peek()]){
int index=stack.pop();
results[index]=i-index;
}
//当日温度小于等于栈顶温度或者栈为空
//存入栈中
stack.push(i);
}
return results;
}
学以致用
- 巧妙将下标存入栈代替温度