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

  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);
        }

你可能感兴趣的:(队列,栈)