Given a list of daily temperatures, produce a list 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 temperatures = [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].

这道题给了我们一个数组,让我们找下一个比当前数字大的数字的距离,我们研究一下题目中给的例子,发现数组是无序的,所以没法用二分法快速定位下一个大的数字,那么最先考虑的方法就是暴力搜索了,写起来没有什么难度,但是OJ并不答应。实际上这道题应该使用递减栈Descending Stack来做,栈里只有递减元素,思路是这样的,我们遍历数组,如果栈不空,且当前数字大于栈顶元素,那么如果直接入栈的话就不是递减栈了,所以我们取出栈顶元素,那么由于当前数字大于栈顶元素的数字,而且一定是第一个大于栈顶元素的数,那么我们直接求出下标差就是二者的距离了,然后继续看新的栈顶元素,直到当前数字小于等于栈顶元素停止,然后将数字入栈,这样就可以一直保持递减栈,且每个数字和第一个大于它的数的距离也可以算出来了,参见代码如下:

class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n, );
stack<int> st;
for (int i = ; i < temperatures.size(); ++i) {
while (!st.empty() && temperatures[i] > temperatures[st.top()]) {
auto t = st.top(); st.pop();
res[t] = i - t;
}
st.push(i);
}
return res;
}
};

类似题目:

Next Greater Element I

参考资料:

https://discuss.leetcode.com/topic/112830/java-easy-ac-solution-with-stack

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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. LeetCode 739. Daily Temperatures (每日温度)

    题目标签:HashMap 题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖.返回一组等待的天数. 可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的in ...

  3. LeetCode 739:每日温度 Daily Temperatures

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

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

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

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

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

  6. 739. Daily Temperatures - LeetCode

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

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

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

  8. LeetCode 739. Daily Temperatures

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

  9. 69.Daily Temperatures(日常气温)

    Level:   Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...

随机推荐

  1. java设计模式------工厂设计模式

    总结 以上就是工厂模式的基本实现和详细说明.包括了简单工厂模式.工厂方法模式.抽象工厂模式.我们可以基于需求来选择合适的工厂模式 基本概念:为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来 ...

  2. python安装的时候报SSL连接错误的解决办法

    Collecting xlwt   Could not fetch URL https://pypi.python.org/simple/xlwt/: There was a problem conf ...

  3. 关于使用Unity开发Kinect时出现的Runtime Error错误的解决方式

    一.开发环境: 1. 硬件:Kinect 2.0 2. 操作系统:Windows10 3. Unity版本:5.x以上 4. Kinect SDK:KinectSDK-v2.0_1409 5. Kin ...

  4. JavaScript(第一天)【<script>标签浅析】

    一.创建一张HTML页面 初学者创建一张html页面建议借助工具,例如Dreamweaver可视化编辑器.   二.<Script>标签解析 <script>xxx</s ...

  5. C语言博客作业--一二维数组。

    一.PTA实验作业 题目1:7-1 将数组中的数逆序存放 1. 本题PTA提交列表 2. 设计思路 定义三个整型变量n用来存放整数个数i,j是循环数 scanf("%d",& ...

  6. Ubuntu下tomcat或eclipse启动提示没有java环境问题

    tomcat和eclipse默认使用了openjdk,通过压缩包安装的jdk无法被识别,通过修改tomcat/bin下的catalina.sh添加jdk和jre路径即可 sudo gedit cata ...

  7. IE bug:ajax请求返回304解决方案

    bug说明: 同一账户下的默认收货地址只有一个,默认收货地址可以修改,修改完成后,使用ajax重新加载收货地址部分. 默认收货地址状态标记:status = 1: 在IE浏览器做了修改后,重新加载的数 ...

  8. stringify 字符串转化成json方法

    参照原文:http://www.cnblogs.com/damonlan/ http://www.jb51.net/article/29893.htm stringify的作用主要是序列化对象(转化为 ...

  9. SpringCloud的Config:ConfigServer注册到EurekaServer中,变成一个Eureka服务

    一.概念与定义 1.将SpringCloud ConfigServer注册到 EurekaServer,以便ConfigClient以服务的方式引用ConfigServer 2.客户端不再引用 Con ...

  10. maven常见问题处理(3-3)Gradle编译时下载依赖失败解决方法

    Gradle编译时在本地仓库中如果没有发现依赖,就会从远程仓库中下载, 默认的远程仓库为 mavenCentral(),即 http://repo1.maven.org/maven2/往往访问速度特别 ...