leetcode基础算法 栈篇 :每日温度(Daily temperatures)

本文介绍了一种使用栈数据结构解决每日气温预测问题的算法。该算法接收一系列每日温度,为每条记录计算直到气温升高所需的天数。通过优化,实现了更高效、简洁的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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].

题目描述:给你每天的温度,计算下一个温度更高的日子距当前的天数。(题目要求栈实现)

 

解题过程的问题:一开始迷糊了蛮长时间,现在一想都不知道当时在想什么,很简单的题想的太麻烦,说明我的在做题的时候脑子里还不是很专注,很清晰。分析解题思路的过程对编程是一个很重要的过程,是既快速又高效解出一道题的前提,我以后要在解体前专注思想,让大脑能更清楚的分析题目。

 

思路:

创建answer数组保存计算的结果值,容量=temperatures.size() 初始值都设为0(0为没有找到“更温暖的一天”)

创建一个栈s来储存那些还未找到“更温暖一天”的值,栈顶的元素一定是最近遍历的且未找到“更温暖一天“的,

例:99 72 72 73 100

所以遍历数组temperatures

如果栈空,把当前元素压入

 tempera数组  →99 72 72 73 100          stack s:99           

到下一个元素时也就是72时  

tempera[i]    99 →72 72 73 100   

此时栈非空  ,将它(72)和栈顶元素比较  72<99  不是栈内元素“更温暖的一天“ 所以压入栈

stack s: 72 99

下一个同理 72=72 压入栈中

stack s: 72 72 99

tempera[i] :99 72 72 →73 100

73>72  是72“更温暖的一天”  所以计算他们之间相差的天数,再存入到answer数组的相应位置上,把栈顶的72 排出 s.pop

answer[0,0,1,0,0], stack s:72 99

此时继续将temperatures[i]同栈顶元素比较,73>72所以重复操作,计算差值存入answer,s.pop

answer [0,2,1,0,0] ,  stack s:99

再比较temperatures[i]和栈顶元素   73<99,所以把73压入栈(因为它不比栈里面的元素大)跳出循环,继续遍历数组temperatures

temperatures:99 72 72 73 →100 ,stack s:73 99

比较100>73 ,计算差值,存入anwer,再比较栈顶元素100>99,同样操作:

stack s:空   answer:4 2 1 1 0

数组遍历到头,计算完成,return ans; 程序结束。

贴上代码(才开始尝试写英文注释,应该有不少错误,发现后改正)

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int>s;
        vector<int>ans(temperatures.size(),0);
        for(int i=0;i<temperatures.size();i++)             // traverse the array [temperatures] ,find the figure that bigger than the one in the top of                                                              // stack[s]
        {
           if(s.empty())
           {
               s.push(temperatures[i]);
               continue;
           }
            if(temperatures[i]>s.top())          //if the figure was found ,find the latest element whose value is 0 from array [ans]
            {                                    //this element is the latest one <temperatures[i] in the array temperatures 
                                                 //if the stack isn't empty ,continue to pop the stack,find the next one < temperatures[i]
                while(!s.empty()) 
                {
                    if(temperatures[i]<=s.top())
                     break;
                    for(int j=i-1;j>=0;j--)
                    {
                        if(ans[j]==0)
                        {
                            ans[j]=i-j;
                            break;
                        } 
                    }
                    s.pop();
                }
                s.push(temperatures[i]);
            }
        else
                       {
                           s.push(temperatures[i]);
                       }
    }
                      
    return ans;
    }
};

 

在leetcode上提交的结果:

运行时间花费属于后百分之40.。。。

看大佬们的提交,最多的也比较高效的是运用了HASH哈希表,由于我还没看到哈希表的部分暂时就不去琢磨了。

前30%~60%的提交就是在栈里存i,也就是天数就行了,到时候一减就ok,我觉得这就应该是普通解了,最正常的解。我一开始理解错题意了,以为答案数组要存的是和最近相差的温度,不然也应该这样写,我改正下:

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

速度快多了,也简洁多了!

果然一个写一个好的程序需要清晰的思路和不断迭代啊!!!啊。

等学到哈希把哈希的代码写在下面:

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值