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. OpenVINO 安装及使用

    安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...

  2. CentOS 7 FTP的安装与配置

    CentOS7 FTP安装与配置 1.FTP的安装 #安装yum install -y vsftpd #设置开机启动systemctl enable vsftpd.service #启动systemc ...

  3. 修改pom项目版本 jenkins 关联 shell命令

    #获取pom文件内的项目版本 version=`awk '/<version>[^<]+<\/version>/{gsub(/<version>|<\/ ...

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

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

  5. 将postgresql中的数据实时同步到kafka中

    参考地址:https://blog.csdn.net/weixin_33985507/article/details/92460419 参考地址:https://mp.weixin.qq.com/s/ ...

  6. nginx-博客阅读笔记记录-20190916

    Nginx 入门学习教程 Ng官网解释: nginx [engine x]是最初由Igor Sysoev编写的HTTP和反向代理服务器,邮件代理服务器和通用TCP / UDP代理服务器. 维基百科解释 ...

  7. php 调用远程url

    // ; Whether to allow the treatment of URLs (like http:// or ftp://) as files. // ; http://php.net/a ...

  8. CSS3选择器 ::selection选择器

    “::selection”伪元素是用来匹配突出显示的文本(用鼠标选择文本时的文本).浏览器默认情况下,用鼠标选择网页文本是以“深蓝的背景,白色的字体”显示的,效果如下图所示: 从上图中可以看出,用鼠标 ...

  9. CSS多列布局(栅格布局)

    一.多列布局 CSS3 可以将文本内容设计成像报纸一样的多列布局,如下实例: 代码如下(具体的解释也在代码中)浏览器支持表格中的数字表示支持该方法的第一个浏览器的版本号. 紧跟在数字后面的 -webk ...

  10. AOP说明

    小程序的开发者或者服务商会提供一些lib库,用来代理小程序的生命周期或API等,进而想要进行一些通用逻辑的处理(例如: 打点/事件触发/统一登录等等通用逻辑). 通常,小程序开发者们使用这些扩展包时, ...