LeetCode 栈和队列(4)

Alex_Shen
2021-02-26 / 0 评论 / 0 点赞 / 66 阅读 / 2,602 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-03-31,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Leetcode 题解 - 栈和队列

232. 用栈实现队列

232. Implement Queue using Stacks (Easy)

Leetcode / 力扣

class MyQueue {
public:
    stack<int> temp,s;

    /** Initialize your data structure here. */
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        while (!s.empty())
        {
            temp.push(s.top());
            s.pop();
        }
        int ans=temp.top();
        temp.pop();
        while (!temp.empty())
        {
            s.push(temp.top());
            temp.pop();
        }
        return ans;
    }
    
    /** Get the front element. */
    int peek() {
        while (!s.empty())
        {
            temp.push(s.top());
            s.pop();
        }
        int ans = temp.top();
        while (!temp.empty())
        {
            s.push(temp.top());
            temp.pop();
        }
        return ans;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s.empty();
    }
};

225. 用队列实现栈

225. Implement Stack using Queues (Easy)

Leetcode / 力扣

class MyStack
{
public:
    queue<int> q, temp;

public:
    /** Initialize your data structure here. */
    MyStack()
    {
    }

    /** Push element x onto stack. */
    void push(int x)
    {
        q.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop()
    {
        while (!temp.empty())
        {
            temp.pop();
        }
        while (q.size()!=1)
        {
            temp.push(q.front());
            q.pop();
        }
        int ans = q.front();
        temp.pop();
        while (!temp.empty())
        {
            q.push(temp.front());
            temp.pop();
        }
        return ans;
    }

    /** Get the top element. */
    int top()
    {
        while (!temp.empty())
        {
            temp.pop();
        }
        while (!q.empty())
        {
            temp.push(q.front());
            q.pop();
        }
        int ans = temp.back();
        while (!temp.empty())
        {
            q.push(temp.front());
            temp.pop();
        }
        return ans;
    }

    /** Returns whether the stack is empty. */
    bool empty()
    {
        return q.empty();
    }
};

155. 最小值栈

155. Min Stack (Easy)

Leetcode / 力扣

思路:在辅助栈中存储对应位置之前的最小元素。

class MinStack
{
public:
    /** initialize your data structure here. */
    stack<int> min_stack;
    stack<int> st;
    MinStack()
    {
        min_stack.push(INT_MAX);
    }

    void push(int x)
    {
        st.push(x);
        min_stack.push(min(x,min_stack.top()));
    }

    void pop()
    {
        st.pop();
        min_stack.pop();
    }

    int top()
    {
        return st.top();
    }

    int getMin()
    {
        return min_stack.top();
    }
};

20. 用栈实现括号匹配

20. Valid Parentheses (Easy)

Leetcode / 力扣

bool isValid(string s)
    {
        stack<char> st;
        st.push('#');
        for (char ch : s)
        {
            if (ch == '(' || ch == '[' || ch == '{')
                st.push(ch);
            else
            {
                if (ch == ')' && st.top() == '(')
                    st.pop();
                else if (ch == ']' && st.top() == '[')
                    st.pop();
                else if (ch == '}' && st.top() == '{')
                    st.pop();
                else
                    return false;
            }
        }
        return st.top() == '#' ? true : false;
    }

739. 数组中元素与下一个比它大的元素之间的距离

739. Daily Temperatures (Medium)

Leetcode / 力扣

思路:单调栈

    vector<int> dailyTemperatures(vector<int> &T)
    {
        int n = T.size();
        vector<int> res(n, 0);
        stack<int> st;
        for (int i = 0; i < T.size(); ++i)
        {
            while (!st.empty() && T[i] > T[st.top()])
            {
                auto t = st.top();
                st.pop();
                res[t] = i - t;
            }
            st.push(i);
        }
        return res;
    }

6. 循环数组中比当前元素大的下一个元素

503. Next Greater Element II (Medium)

Leetcode / 力扣

项目地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97.md

0

评论区