寒假刷题打卡第十五天 | 栈 & 队列

  1. 用栈实现队列
    思路没什么好说的。只是有个问题,为何我在构造函数中定义两个栈会被报错。
class MyQueue {
public:
    /** Initialize your data structure here. */
    stack<int> s1, s2;
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        if(s2.size())
        {
            int temp = s2.top();
            s2.pop();
            return temp;
        }
        else
        {
            while(s1.size())
            {
                int temp = s1.top();
                s2.push(temp);
                s1.pop();
            }
            int temp = s2.top();
            s2.pop();
            return temp;
        }
    }
    
    /** Get the front element. */
    int peek() {
        if(s2.size())
            return s2.top();
        else
        {
            while(s1.size())
            {
                int temp = s1.top();
                s2.push(temp);
                s1.pop();
            }
            return s2.top();
        }
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty()&&s2.empty();
    }
};
  1. 用队列实现栈
    用一个队列实现。
class MyStack {
public:
    /** Initialize your data structure here. */
    queue<int> q;
    MyStack() {
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        int n = q.size();
        q.push(x);
        while(n--)
        {
            int temp = q.front();
            q.pop();
            q.push(temp);
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int temp = q.front();
        q.pop();
        return temp;
    }
    
    /** Get the top element. */
    int top() {
        return q.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return q.empty();
    }
};
  1. 最小值栈
  2. 每日温度
    类似题目:接雨水柱状图最大矩形
    用单调栈
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        int n = T.size();
        vector<int> ans(n); //初始化为0
        stack<int> s;
        for (int i = 0; i < n; ++i) {
            while (!s.empty() && T[i] > T[s.top()]) {
                int previousIndex = s.top();
                ans[previousIndex] = i - previousIndex;
                s.pop();
            }
            s.push(i);
        }
        return ans;
    }
};
  1. 循环数组中的下一个更大元素
    参考上一题,应用单调栈。
class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        int n = nums.size();
        vector<int> ans(n, -1);
        stack<int> s;
        for(int i=0; i<n; i++)
        {
            while(!s.empty() && nums[i]>nums[s.top()])
            {
                ans[s.top()] = nums[i];
                s.pop();
            }
            s.push(i);
        }
        for(int i=0; i<n; i++)
        {
            while(!s.empty() && nums[i]>nums[s.top()])
            {
                ans[s.top()] = nums[i];
                s.pop();
            }
            s.push(i);
        }
        return ans;
    }
};

上面的两个for循环可以写成一个

        for(int i=0; i<2*n; i++)
        {
            while(!s.empty() && nums[i%n]>nums[s.top()])
            {
                ans[s.top()] = nums[i%n];
                s.pop();
            }
            s.push(i%n);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值