当前位置:网站首页>【Hot100】739. 每日温度
【Hot100】739. 每日温度
2022-07-06 06:42:00 【王六六的IT日常】
739. 每日温度
给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。
输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]
题目理解:
对于输入 73,它需要 经过一天 才能等到温度的升高,也就是在第二天的时候,温度升高到 74 ,所以对应的结果是 1。
对于输入 74,它需要 经过一天 才能等到温度的升高,也就是在第三天的时候,温度升高到 75 ,所以对应的结果是 1。
对于输入 75,它经过 1 天后发现温度是 71,没有超过它,继续等,一直 等了四天,在第七天才等到温度的升高,温度升高到 76 ,所以对应的结果是 4 。
对于输入 71,它经过 1 天后发现温度是 69,没有超过它,继续等,一直 等了两天,在第六天才等到温度的升高,温度升高到 72 ,所以对应的结果是 2 。
对于输入 69,它 经过一天 后发现温度是 72,已经超过它,所以对应的结果是 1 。
对于输入 72,它 经过一天 后发现温度是 76,已经超过它,所以对应的结果是 1 。
对于输入 76,后续 没有温度 可以超过它,所以对应的结果是 0 。
对于输入 73,后续 没有温度 可以超过它,所以对应的结果是 0 。
想法:针对每个温度值 向后进行依次搜索 ,找到比当前温度更高的值,这是最容易想到的办法。
原理:是从左到右除了最后一个数其他所有的数都遍历一次,最后一个数据对应的结果肯定是 0,就不需要计算。
遍历的时候,每个数都去向后数,直到找到比它大的数,数的次数就是对应输出的值。
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int len = temperatures.length;
int[] res = new int[len];
for(int i=0;i<len;i++){
int cur = temperatures[i];
if(cur < 100){
for(int j=i+1;j<len;j++){
if(temperatures[j] > cur){
res[i] = j-i;
break;
}
}
}
}
return res;
}
}
用栈来解决:
递减栈 :栈里只有递减元素。
遍历整个数组,如果栈不空,且当前数字大于栈顶元素,那么如果直接入栈的话就不是 递减栈 ,所以需要取出栈顶元素,由于当前数字大于栈顶元素的数字,而且一定是第一个大于栈顶元素的数,直接求出下标差就是二者的距离。
继续看新的栈顶元素,直到当前数字小于等于栈顶元素停止,然后将数字入栈,这样就可以一直保持递减栈,且每个数字和第一个大于它的数的距离也可以算出来。
class Solution {
public int[] dailyTemperatures(int[] T) {
Stack<Integer> stack = new Stack<>();
int length = T.length;
int[] result = new int[length];
for (int i = 0; i < length; i++) {
while (!stack.isEmpty() && T[i] > T[stack.peek()]) {
int pre = stack.pop();
result[pre] = i - pre;
}
stack.add(i);
}
return result;
}
}
边栏推荐
- After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
- Leetcode daily question (1997. first day where you have been in all the rooms)
- Suspended else
- Automated test environment configuration
- Office-DOC加载宏-上线CS
- mysql的基础命令
- SQL Server manager studio(SSMS)安装教程
- 我的创作纪念日
- How do programmers remember code and programming language?
- Day 248/300 thoughts on how graduates find jobs
猜你喜欢
万丈高楼平地起,每个API皆根基
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
Apache DolphinScheduler源码分析(超详细)
Office doc add in - Online CS
Reflex WMS medium level series 3: display shipped replaceable groups
Monotonic stack
SAP SD发货流程中托盘的管理
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
随机推荐
Automated test environment configuration
CS passed (cdn+ certificate) PowerShell online detailed version
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Lesson 7 tensorflow realizes convolutional neural network
Apache DolphinScheduler源码分析(超详细)
电子书-CHM-上线CS
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
端午节快乐Wish Dragon Boat Festival is happy
My creation anniversary
E-book CHM online CS
The internationalization of domestic games is inseparable from professional translation companies
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
Summary of leetcode's dynamic programming 4
pymongo获取一列数据
Biomedical localization translation services
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
SQL Server Manager studio (SSMS) installation tutorial
How to translate professional papers and write English abstracts better
Successfully solved typeerror: data type 'category' not understood
Lecture 8: 1602 LCD (Guo Tianxiang)