当前位置:网站首页>【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;
}
}
边栏推荐
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
- 基于PyTorch和Fast RCNN快速实现目标识别
- LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
- Number of query fields
- MySQL5.72.msi安装失败
- Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
- C语言_双创建、前插,尾插,遍历,删除
- 如何做好互联网金融的英语翻译
- UniPro甘特图“初体验”:关注细节背后的多场景探索
- LeetCode - 152 乘积最大子数组
猜你喜欢

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm

红蓝对抗之流量加密(Openssl加密传输、MSF流量加密、CS修改profile进行流量加密)

On the first day of clock in, click to open a surprise, and the switch statement is explained in detail

How to translate biomedical instructions in English

机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm

After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious

Leetcode daily question (971. flip binary tree to match preorder traversal)

Fedora/rehl installation semanage

26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
随机推荐
Leetcode - 152 product maximum subarray
[unity] how to export FBX in untiy
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
CS通过(CDN+证书)powershell上线详细版
Simple query cost estimation
Wish Dragon Boat Festival is happy
Office-DOC加载宏-上线CS
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
医疗软件检测机构怎么找,一航软件测评是专家
论文翻译英译中,怎样做翻译效果好?
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
mysql的基础命令
C语言_双创建、前插,尾插,遍历,删除
Biomedical localization translation services
【刷题】怎么样才能正确的迎接面试?
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
Leetcode daily question (971. flip binary tree to match preorder traversal)
云上有AI,让地球科学研究更省力
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower