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. 简单生成随机数id的方法

    近期项目中需要生成不同的id,之前都是使用UUID来实现的,现在需求是只要8位数的,也就是说用户量是有限的,暂时是不需要太多的,所以就简单的使用Set实现了这一功能. /** * 生成8位不重复随机i ...

  2. 线程工具类 - CountDownLatch(倒计时器)

    CountDownLatch官方文档 一.原理 CountDownLatch是一个非常实用的多线程控制工具类.Count Down在英文中意为倒计时,Latch意为门闩,可以简单的将CountDown ...

  3. RedisTemplate 事务处理方法 watch multi exec 的使用

    @Autowired RedisTemplate<String,String> redisTemplate; redisTemplate.execute(new SessionCallba ...

  4. vuex的mapState方法来获取vuex的state对象中属性

    有两种写法 1.首先在组件中引入vuex的mapState方法: 首先在组件中引入vuex的mapState方法: import { mapState } from 'vuex' 然后在compute ...

  5. 对webpack的初步研究4

    Mode string module.exports = { mode: 'production' }; webpack --mode=production The following string ...

  6. 如何分析及处理 Flink 反压?

    反压(backpressure)是实时计算应用开发中,特别是流式计算中,十分常见的问题.反压意味着数据管道中某个节点成为瓶颈,处理速率跟不上上游发送数据的速率,而需要对上游进行限速.由于实时计算应用通 ...

  7. OC + RAC (三) 信号中的信号

    -(void)_test3{ RACSubject *signalofsignal = [RACSubject subject]; //信号中的信号(也就是发送的数据是信号) RACSubject * ...

  8. git 常用命令记录

    删除远程分支 git push origin --delete 远程分支名 删除本地分支 git branch -d 本地分支名 从master新建分支 git checkout -b 新分支名 建立 ...

  9. Axios 安卓4.4不兼容的问题

    问题:Vue在使用Axios做接口请求时,如果是安卓4.4系统会发生报错,原因是安卓4.4不支持ES6的Promise语法 解决方案: 1.安装: npm install babel-polyfill ...

  10. Ckeditor IE下粘贴word中图片问题

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...