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. [Chrome 浏览器快捷键]——“你是键盘党吗?”

    标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 在隐身模式下打开新窗口 Ctrl + Shift + n 打开新的标签页,并跳转到该标签页 Ctrl + t 重新打开最后关闭的标签页, ...

  2. Intellij Idea下tomcat设置自动编译

    *eclipse默认tomcat下是自动完成编译:而Intellij Idea默认tomcat下不是自动完成编译,从如下开始设置: 进入"settings",如下图找到" ...

  3. 关于ORM,以及Python中SQLAlchemy的scoped_session

    orm(object relational mapping):对象关系映射. python面向对象,而数据库是关系型. orm是将数据库关系映射为Python中的对象,不用直接写SQL. 缺点是性能略 ...

  4. router问题

    var http = require("http"); var router = require("./router.js"); //创建服务器 var ser ...

  5. 学生ID查询

    var http = require("http"); var server = http.createServer(function(req,res){ //得到url var ...

  6. android 广播安装指定下载的apk

    // 广播出去,由广播接收器来处理下载完成的文件   Intent sendIntent = new Intent("com.test.downloadComplete");    ...

  7. Exception in thread "main" expected '<document start>', but found BlockMappingStart in 'reader', line 23, column 2: nimbus.host: "master"

    平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6   hadoop 1.2.1 启动storm的时候,遇到这个问题,百度之后,看到大家的解决方案 ...

  8. Linux下Apache服务的查看和启动

      cd到/etc/rc.d/init.d/目录,并列出该目录下的所有文件,看看是否有httpd   使用httpd -v查看已经安装的httpd的版本   使用rpm -qa | grep http ...

  9. 如何使用ILAsm与ILDasm修改.Net exe(dll)文件

    一.背景 最近项目组新上项目,交付的时间比较急迫,原本好的分支管理习惯没有遵守好,于是出现下面状况: 多个小伙伴在不同的分支上开发. 原本QA环境也存在一个阻碍性的bug A 一位同事在QA环境发布了 ...

  10. Java中Math类的常用方法

    public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println( ...