[栈] 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; i < n; i++)
        {
            while (!sta.empty() && T[i] > T[sta.top()])
            {
                res[sta.top()] = i - sta.top();
                sta.pop();
            }
            sta.push(i);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11268275.html

时间: 2024-11-04 04:59:20

[栈] leetcode 739 Daily Temperatures的相关文章

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

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

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

[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 - 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 402 Remove K Digits

problem:https://leetcode.com/problems/remove-k-digits 单调栈.维护一个递增的栈,每pop一次意味着移除了一个元素,k--.减为0时不再移除.前导0处理起来很麻烦,很容易WA. class Solution { public: string removeKdigits(string num, int k) { string res; for(int i = 0;i < num.size(); i++) { if(res.size() &&a

[栈] leetcode 1019 Next Greater Node In Linked List

problem:https://leetcode.com/problems/next-greater-node-in-linked-list/ 维护递减的单调栈.这道题对象是链表,不像数组可以快速通过下标索引,所以比较方便的做法是在栈中同时记录数字和对应的下标,并且默认填0,如果找到了比它大的第一个数,再修改下标对应的数字. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne

LeetCode | 739.每日温度

根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]. 提示:气温 列表长度的范围是 [1, 30000].每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数. solution: 什么是单调栈? 单调栈分

[栈] leetcode 503 Next Greater Element II

problem:https://leetcode.com/problems/next-greater-element-ii/ 一道比较简单的单调队列题目.不过由于题目要求是循环的,需要两个pass,第二个pass处理循环生效的next greater,同时需要把下标已经超出范围的队首数据及时pop出来. class Solution { public: vector<int> nextGreaterElements(vector<int>& nums) { deque<