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.size() -1; i >= 0; i--) {
            while (!st.empty() && temperatures[i] >= temperatures[st.top()])
                st.pop();
            if (st.empty())
                res[i] = 0;
            else
                res[i] = st.top() - i;
            st.push(i);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/JTechRoad/p/8998068.html

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

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

[栈] 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

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-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 Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

arts-week2

Algorithm 739. Daily Temperatures - LeetCode 535. Encode and Decode TinyURL - LeetCode 811. Subdomain Visit Count - LeetCode 706. Design HashMap - LeetCode 771. Jewels and Stones - LeetCode 204. Count Primes - LeetCode 445. Add Two Numbers II - LeetC

Leetcode题解——数据结构之栈和队列

1. 用栈实现队列 2. 用队列实现栈 3. 最小值栈 4. 用栈实现括号匹配 5. 数组中元素与下一个比它大的元素之间的距离 6. 循环数组中比当前元素大的下一个元素 1. 用栈实现队列 232. Implement Queue using Stacks (Easy) 栈的顺序为后进先出,而队列的顺序为先进先出.使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序. class MyQueue { private