[LeetCode] Daily Temperatures 日常温度

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, 0);
        stack<int> st;
        for (int i = 0; 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 题目讲解汇总(持续更新中...)

时间: 2024-08-30 17:58:12

[LeetCode] Daily Temperatures 日常温度的相关文章

LeetCode - Daily Temperatures

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

LeetCode 739:每日温度 Daily Temperatures

题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]. Given a list of daily temperatures T, return a list such that, for each day in

739. Daily Temperatures - LeetCode

Question 739.?Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: public int[] dailyTemperatures(int[] temperatures) { int[] ans = new int[temperatures.length]; for (int i = 0; i<temperatures.length; i++) { int highId

[栈] leetcode 739 Daily Temperatures

problem:https://leetcode.com/problems/daily-temperatures/ 一道使用单调栈维护(递减序列)的题目,比较简单. class Solution { public: vector<int> dailyTemperatures(vector<int>& T) { stack<int> sta; int n = T.size(); vector<int> res(n, 0); for (int i = 0

739. Daily Temperatures

https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int> dailyTemperatures(vector<int>& temperatures) { stack<int> st; vector<int> res(temperatures.size()); for (int i = temperatures.si

【daily】日常所遇 - 页面A嵌套页面B

因为懒,所以直接在http://www.runoob.com写demo测试了. (1)iframe嵌套 虽然可能到现在这中方式都很常用,但是你baidu/google一下会发现.很多人都并不提倡用iframe. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <

LeetCode-739 Daily Temperatures Solution (with Java)

1. Description: 2.Solutions: 1 /** 2 * Created by sheepcore on 2019-05-07 3 */ 4 class Solution { 5 public int[] dailyTemperatures(int[] T) { 6 int[] res = new int[T.length]; 7 for(int i = 0; i < T.length -1; i++){ 8 boolean hasWarmerDay = false; 9 f

Leetcode daily 20/03/23 链表的中间结点

链表的中间结点 -题目- 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. -示例1- 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4,5]) 返回的结点值为 3 . (测评系统对该结点序列化表述是 [3,4,5]). 注意,我们返回了一个 ListNode 类型的对象 ans,这样: ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans

LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组?nums1 和?nums2,其中 nums1?是?nums2?的子集.找到?nums1?中每个元素在?nums2?中的下一个比其大的值. nums1?中数字?x?的下一个更大元素是指?x?在?nums2?中对应位置的右边的第一个比?x?大的元素.如果不存在,对应位置输出 -1. 每日一算法2019/6/7Day 35LeetCode496. Next Greater Ele