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. Luogu P5018 对称二叉树 瞎搞树&哈希

    我的天..普及组这么$hard$... 然后好像没有人用我的垃圾做法,,,好像是$O(n)$,但十分的慢,并且极其暴力$qwq$ 具体来说,就是直接$dfs$求出树高,然后想像出把原来的树补成满二叉树 ...

  2. 【CUDA 基础】5.6 线程束洗牌指令

    title: [CUDA 基础]5.6 线程束洗牌指令 categories: - CUDA - Freshman tags: - 线程束洗牌指令 toc: true date: 2018-06-06 ...

  3. Linux命令行学习日志-ps ax

    当我们需要查询某个运行中的进程的时候,这个命令就显得很有用了,可以查看当前进程的PID和状态(S代表睡眠,SW代表睡眠和等待,R表示运行中) ps ax //查看当前运行中的进程

  4. 在ABP core中使用RabbitMq

    距上一篇博客的更新一集很久了,主要是最近做的事情比较杂,中间也有一个难点,就是在ABP中加入APP扫码登录,本来想些的,但是觉得这个写出来会不会让我们的系统被破解-_-||,所以想了想,就没有写. 这 ...

  5. C#_选择结构,Console的应用,数据类型转换

    1:先看一个顺序结构编程,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.T ...

  6. TCP拥塞控制算法

    转自浅谈TCP拥塞控制算法 本篇文章介绍了几种经典的TCP拥塞控制算法,包括算法原理及各自适用场景. 回顾上篇文章:浅谈 redis 延迟 前言 TCP 通过维护一个拥塞窗口来进行拥塞控制,拥塞控制的 ...

  7. LeetCode 279. 完全平方数(Perfect Squares)

    题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释 ...

  8. koa 允许跨域

    1.安装模块 npm install koa2-cors --save 2.引用 const cors = require('koa2-cors'); ... // 允许跨域访问 app.use(co ...

  9. [Java读书笔记] Effective Java(Third Edition) 第 3 章 对于所有对象都通用的方法

    第 10 条:覆盖equals时请遵守通用约定 在不覆盖equals方法下,类的每个实例都只与它自身相等. 类的每个实例本质上都是唯一的. 类不需要提供一个”逻辑相等(logical equality ...

  10. Jmeter设置字体大小

    Jmeter5.0原配置字体很小,需要更改其配置 在apache-jmeter-5.0/bin/下的jmeter.properties文件中添加如下内容: jmeter.hidpi.mode=true ...