69.Daily Temperatures(日常气温)
Level:
Medium
题目描述:
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
思路分析:
设置一个栈,栈中保存的元素为对应天数和它当日的气温,遍历气温数组,当栈为空时,将第一天的天数,即下标和其对应的气温压入栈中,然后判断后来的元素的气温是否大于栈顶元素的气温,如果大于,那么弹出栈顶元素得到弹出元素和当前元素的相差天数,保存为弹出元素的结果,负责将当前元素压入栈,继续向下进行遍历。
代码:
public class Solution{
public int []dailyTemperatures(int []T){
Stack<int []>s=new Stack<>();//存放下标和其对应的温度
int []res=new int [T.length];
for(int i=0;i<T.length;i++){
while(!s.isEmpty()&&s.peek()[1]<T[i]){
int []temp=s.pop();
res[temp[0]]=i-temp[0];//相差的天数
}
s.push(new int[]{i,T[i]});
}
return res;
}
}
69.Daily Temperatures(日常气温)的更多相关文章
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- [Swift]LeetCode739. 每日温度 | Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- [Leetcode 739]*还有几天会升温 Daily Temperatures
[题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...
- LeetCode - Daily Temperatures
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- Leetcode739 - Daily Temperatures
题目描述 Leetcode 739 本题考察了栈的使用.题目输入是一段温度值列表,然后返回一个列表.这个列表包含了输入列表中每一天还有多少天温度升高.如果未来没有升高的情况,则输入 0. # Exam ...
- Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
随机推荐
- 一、苹果Assets.car文件解析图片
一. https://blog.wxhbts.com/assets.html
- 未来HTML5的发展前景如何?黑客专家是这样回答的
如果你想进军IT行业,如果你准备好掌握一项新技术,那么就选择HTML5.近日,我们采访了国内知名网络黑客安全专家郭盛华,帮助您了解当今最重要的技术.在本篇文章中,黑客安全专家郭盛华回答了有关HTML5 ...
- sed 搜索并替换
find . -type f -exec sed -i "s/std=c++11/std=c++14/g" {} \; 搜索当前目录下的文件,把std=c++11替换成std=c+ ...
- 【leetcode】1042. Flower Planting With No Adjacent
题目如下: You have N gardens, labelled 1 to N. In each garden, you want to plant one of 4 types of flow ...
- TPS、QPS和系统吞吐量的区别和理解
参考:https://blog.csdn.net/u010889616/article/details/83245695 一.QPS/TPSQPS:Queries Per Second意思是“每秒查询 ...
- vivo面试题
0.自动拆箱和装箱 java有8种原始类型,分为数字型,字符型,布尔型.其中数字型又分为整数型和浮点数型.整数型按照占用字节数从小到大依次是byte(占用1个字节,值范围是[-27 ~ 27-1]). ...
- bzoj1964: hull 三维凸包
传送门 二维平面四个点求凸包面积->任选三个点面积之和/2 三维平面五个点求凸包体积->任选四个点体积之和/2 二维平面三个点面积->二个二维向量行列式值的绝对值/2 三维平面四个点 ...
- 添加对象到 HashSet 里的规则是
下面的解释取自百度知道的一位网友的回答,链接如下: java HashSet类添加元素的问题_百度知道http://zhidao.baidu.com/link?url=9bcAnolev1EBeFI_ ...
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- Js获取屏幕宽度、高度
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...