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(日常气温)的更多相关文章

  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

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

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

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

  5. LeetCode - Daily Temperatures

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

  6. LeetCode 739. Daily Temperatures

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

  7. Leetcode739 - Daily Temperatures

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

  8. Daily Temperatures

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

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

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

随机推荐

  1. tomcat启动报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

      windows系统: 部署了一个Tomcat8.5.15,bin目录下startup.bat执行,结果提示Neither the JAVA_HOME nor the JRE_HOME enviro ...

  2. python特殊的类属性

    类C的特殊属性: C.__name__ 类C的名字 C.__doc__ 类C文档字符串 C.__bases__ 类C所有父类的元组 C.__dict__ 类C的属性 C.__module__ 类C所在 ...

  3. uboot学习之五-----uboot如何启动Linux内核

    uboot和内核到底是什么?uboot实质就是一个复杂的裸机程序:uboot可以被配置也可以做移植: 操作系统内核本身就是一个裸机程序,和我们学的uboot和其他裸机程序没有本质的区别:区别就是我们操 ...

  4. 去除重复嵌套的html标签函数

    去除重复嵌套的html标签 function strip_multi_tags($str, $tag = 'div'){ preg_match_all('/<'.$tag.'>|<\ ...

  5. 企业级监控软件Zabbix搭建部署之zabbix在WEB页面中的配置

    企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 关于安装请看 http://www.linuxidc.com ...

  6. hdu 6053: TrickGCD (2017 多校第二场 1009) 【莫比乌斯 容斥原理】

    题目链接 定义f[n]表示n是最大公约数情况下的计数,F[n]为n是公约数情况下的计数 (可以和 http://www.cnblogs.com/Just--Do--It/p/7197788.html  ...

  7. __getattr__属性查找

    from datetime import date """ __getattr__ : 在查找不到对象的属性时调用 __getattribute__ : 在查找属性之前调 ...

  8. 02 IDEA创创建第一个maven工程

    导入坐标

  9. 20180803-Java 流(Stream)、文件(File)和IO

    Java 流(Stream).文件(File)和IO 下面的程序示范了用read()方法从控制台不断读取字符直到用户输入"q". // 使用BufferedReader 在控制台读 ...

  10. 新建 SecondFragment 实现类

    package com.test.mvp.mvpdemo.mvp.v6.view; import android.os.Bundle;import android.support.annotation ...