Daily Temperatures
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].
分析:
为了找到后面过了几天会有比当前值更大的值,所以,我们需要从尾遍历,我们用一个stack来存当前最大值。
public class Solution {
public int[] dailyTemperatures(int[] T) {
if (T == null) return null;
int[] result = new int[T.length];
Stack<Temperature> stack = new Stack<>();
for (int i = T.length - ; i >= ; i--) {
if (stack.isEmpty()) {
result[i] = ;
stack.push(new Temperature(T[i], i));
} else if (stack.peek().value > T[i]) {
result[i] = stack.peek().index - i;
stack.push(new Temperature(T[i], i));
} else {
while (!stack.isEmpty() && stack.peek().value <= T[i]) {
stack.pop();
}
if (stack.isEmpty()) {
result[i] = ;
} else {
result[i] = stack.peek().index - i;
}
stack.push(new Temperature(T[i], i));
}
}
return result;
}
}
class Temperature {
int value;
int index;
public Temperature(int value, int index) {
this.value = value;
this.index = index;
}
}
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
[题目] 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 ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- Leetcode739 - Daily Temperatures
题目描述 Leetcode 739 本题考察了栈的使用.题目输入是一段温度值列表,然后返回一个列表.这个列表包含了输入列表中每一天还有多少天温度升高.如果未来没有升高的情况,则输入 0. # Exam ...
- 69.Daily Temperatures(日常气温)
Level: Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
随机推荐
- 洛谷【P2257】 YY的GCD
出处:http://www.cnblogs.com/peng-ym/p/8652288.html ( 直接去出处那看就好了 ) 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求 ...
- CSP-S 模拟测试57题解
人生第一次A,B层一块考rank2,虽然说分差没几分,但还是值得纪念. 题解: T1 天空龙: 大神题,因为我从不写快读也没有写考场注释的习惯,所以不会做,全hzoi就kx会做,kx真大神级人物. T ...
- 新西达电调初始化代码,使用nodejs ffi技术调用wiringpi,代码使用typescript编写
这是我设计的F450四轴飞行器飞控代码的一部分 运行在orangepi-zero上,操作系统是armbian,思路是使用node-ffi调用wiringpi的so库与GPIO通信,然后控制端逻辑代码使 ...
- Love to be loved by you & Just one last dance
http://baike.baidu.com/link?url=wOnBuPncIH5b5oWc0ZREXCU8x6XPYqlZazTLarTjE8eOpdtpv57YMeB_kgXQq4BcCeh2 ...
- K - Kia's Calculation(贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDFS 特殊权限位
HDFS 特殊权限位 标签(空格分隔): Hadoop 之前对HDFS更或者说是对Linux中文件的权限没有进行一个完整的学习,只是知道有所有者.所属组和其它权限,具体到某个人的权限有读(r).写(w ...
- hadoop HA+Federation(高可用联邦)搭建配置(一)
hadoop HA+Federation(高可用联邦)搭建配置(一) 标签(空格分隔): 未分类 介绍 hadoop 集群一共有4种部署模式,详见<hadoop 生态圈介绍>. HA联邦模 ...
- 队列(Java实现)
队列的特点是先进先出. 基于链表的队列 public class LinkedListQueue<Item> { private Node first; // 指向最早添加进队列的元素 p ...
- Mysql数据表字段扩充的小技巧
在开发中,往往需求变更比开发速度要快,就会存在一些问题,比如突然要增加一个字段,我们需要 alter table 表名 add [column] 字段名 数据类型 [列属性] [位置]; 然后修改实体 ...
- laravel如何从mysql数据库中随机抽取n条数据
laravel如何从mysql数据库中随机抽取n条数据 一.总结 一句话总结: inRandomOrder():$userQuestions=UserQuestion::where($map)-> ...