【题目】

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].

还有几天会升温。

https://leetcode.com/problems/daily-temperatures

【思路】

暴力求解,用时太长了……参考答案用stack

【代码】

堆栈,倒序比较,当

1、stack不为空,T[i-1]>T[i],证明不会升温,pop出栈跳出。进行步骤2

2、判断stack是否为空,空栈无条件满足返回0,否则【顶点(升温点)-循环数i 】为天数

3、把i压入栈。

class Solution {
public int[] dailyTemperatures(int[] T) {
int[] ans = new int[T.length];
Stack<Integer> stack = new Stack();
for (int i = T.length - 1; i >= 0; --i) {
while (!stack.isEmpty() && T[i] >= T[stack.peek()])
stack.pop();
ans[i] = stack.isEmpty() ? 0 : stack.peek() - i;
stack.push(i);
}
return ans;
}
}

方便理解

class Solution {
public int[] dailyTemperatures(int[] T) {
int tmp[]=new int[T.length];
Arrays.fill(tmp, 1);
tmp[T.length-1]=0;
for(int i=0;i<T.length-1;i++){
int j=i+1;
while(T[j]<=T[i]){
++j;
tmp[i]++;
if(j>=T.length){
break;
}
}
tmp[i]=j<T.length?tmp[i]:0;
}
return tmp;
}
}

[Leetcode 739]*还有几天会升温 Daily Temperatures的更多相关文章

  1. 739. Daily Temperatures - LeetCode

    Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...

  2. LeetCode 739. Daily Temperatures

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

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

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

  4. LeetCode 739:每日温度 Daily Temperatures

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

  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 - Daily Temperatures

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

  7. Leetcode739 - Daily Temperatures

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

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

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

  9. Daily Temperatures

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

随机推荐

  1. 日常英语---四、vis.js是什么

    日常英语---四.vis.js是什么 一.总结 一句话总结:A dynamic, browser based visualization library. 动态基于浏览器的可视库 http://vis ...

  2. Docker用途 & 和tomcat的区别

    两者不是同一种类型.1.docker 是容器,tomcat是jsp应用服务器2.tomcat可以安装在物理机上,虚拟机上,也可以安装在Docker上.所以从这个角度讲,Docker也可以看做是一种超轻 ...

  3. Spring Boot入门第五天:使用JSP

    原文链接 1.在pom.xml文件中添加依赖: <!-- Servlet依赖 --> <dependency> <groupId>javax.servlet< ...

  4. English trip M1 - PC6 Likes and Dislike Teacher:Jade

    In this lesson you will learn to talk about likes and dislikes. 课上内容(Lesson) # 通常在习惯性的表达式用 it's 来表达w ...

  5. C# 网页图片采集

    http://blog.csdn.net/a237428367/article/details/5987832 using System; using System.Collections.Gener ...

  6. android-------- 多渠道打包(借助友盟移动统计分析)

    好久没有发博客了,原因是换工作了,今天端午假期,所以来发一篇博客, 多渠道打包,借助友盟移动统计分析,希望对各位有所帮助 多渠道打包的理解: 渠道包就是要在安装包中添加渠道信息,也就是channel, ...

  7. JavaScript基础一

    1.1 javascript简介 Web前端有三层: HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) J ...

  8. spring boot(十一)MongoDB的使用

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  9. strom:实时的WordCount

    集采单词 package wordcount; import java.io.File; import java.io.IOException; import java.util.Collection ...

  10. jQuery生成二维码 jquery.qrcode.js

    https://github.com/jeromeetienne/jquery-qrcode 1.将jquery.qrcode.min.js和jquery添加到您的网页中 <script src ...