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的更多相关文章

  1. [LeetCode] Daily Temperatures 日常温度

    Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...

  2. [Swift]LeetCode739. 每日温度 | Daily Temperatures

    Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...

  3. [Leetcode 739]*还有几天会升温 Daily Temperatures

    [题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...

  4. LeetCode - Daily Temperatures

    Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...

  5. LeetCode 739. Daily Temperatures

    原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...

  6. LeetCode 739:每日温度 Daily Temperatures

    题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...

  7. Leetcode739 - Daily Temperatures

    题目描述 Leetcode 739 本题考察了栈的使用.题目输入是一段温度值列表,然后返回一个列表.这个列表包含了输入列表中每一天还有多少天温度升高.如果未来没有升高的情况,则输入 0. # Exam ...

  8. 69.Daily Temperatures(日常气温)

    Level:   Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...

  9. 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...

随机推荐

  1. hdu 5869 Different GCD Subarray Query BIT+GCD 2016ICPC 大连网络赛

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  2. 源码安装ROS Melodic Python3 指南 (转) + 安装记录

    这篇文章转自   https://blog.csdn.net/id9502/article/details/80410989  csdn真是作大死,我保存这篇博客的时候还不需要花钱就能看,现在居然要v ...

  3. Postman实现数字签名,Session依赖, 接口依赖, 异步接口结果轮询

    Script(JS)为Postman赋予无限可能 基于Postman 6.1.4 Mac Native版 演示结合user_api_demo实现 PS 最近接到任务, 要把几种基本下单接口调试和持续集 ...

  4. Django从Models 10分钟定制一个Admin后台

    目录 Django从Models 10分钟建立一套RestfulApi Django从Models 10分钟定制一个Admin后台 简介 Django自带一个Admin后台, 支持用户创建,权限配置和 ...

  5. ACM之路(17)—— 博弈论

    博弈论这方面网上资料庞大,我觉得我不可能写的比他们好,就转载一下我觉得写的不错的博客好了. 首先是三大博弈:巴什博奕,威佐夫博奕,尼姆博奕.博客:三大基本博弈. 然后是强大的sg函数和sg定理:SG. ...

  6. 1.4 JAVA日期处理

    一.JAVA日期 参考链接:https://www.runoob.com/java/java-date-time.html 1.日期两个构造函数 1.第一个构造函数使用当前日期和时间来初始化对象.Da ...

  7. iOS 8 - Can't Install Enterprise App

    http://stackoverflow.com/questions/25733299/ios-8-cant-install-enterprise-app 版权声明:本文为博主原创文章,未经博主允许不 ...

  8. Leetcode题目152.乘积最大子序列(动态规划-中等)

    题目描述: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6 ...

  9. Jinja2学习

    模板: 知名模板引擎:jinja2(flask默认的模板引擎) Mako template模板路径: 1.在渲染模板的时候,会默认从根目录下的templates目录下查找模板文件 2.也可以自定义模板 ...

  10. C# Read/Write another Process' Memory

    https://codingvision.net/security/c-read-write-another-process-memory Today’s tutorial is about…proc ...