栈与队列01

232. 用栈实现队列

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    MyQueue() {
        
    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if(stOut.empty())
        {
            while(!stIn.empty())
            {
                int x = stIn.top();
                stIn.pop();
                stOut.push(x);
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }
    
    int peek() {
        int x = this -> pop();
        stOut.push(x);
        return x;
    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

225. 用队列实现栈

class MyStack {
public:
    queue<int> q;
   
    MyStack() {
        
    }
    
    void push(int x) {
        q.push(x);
    }
    
    int pop() {
        int size = q.size() - 1;
        while(size--)
        {
            q.push(q.front());
            q.pop();
        }
        int x = q.front();
        q.pop();
        return x;
    }
    
    int top() {
        int x = this-> pop();
        q.push(x);
        return x;
    }
    
    bool empty() {
        return q.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

20. 有效的括号

class Solution {
public:
    bool isValid(string s) {
        stack<char> ch;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == '(' || s[i] == '{' || s[i] == '[')
                ch.push(s[i]);
            else
            {
                if(s[i] == ')')
                {
                    if(!ch.empty() && ch.top() == '(')
                    {
                        ch.pop();
                        continue;
                    }
                    else
                        return false;
                }
                if(s[i] == '}')
                {
                    if(!ch.empty() && ch.top() == '{')
                    {
                        ch.pop();
                        continue;
                    }
                    else
                        return false;
                }
                if(s[i] == ']')
                {

                    if(!ch.empty() && ch.top() == '[')
                    {
                        ch.pop();
                        continue;
                    }
                    else
                        return false;
                }
            }
        }
        if(ch.empty())
            return true;
        else
            return false;
    }
};

1047. 删除字符串中的所有相邻重复项

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for(int i = 0; i < s.size(); i++)
        {
            if(!st.empty() && s[i] == st.top())
            {
                st.pop();
            }
            else
            {
                st.push(s[i]);
            }
        }
        string result;
        while(!st.empty())
        {
            result.push_back(st.top());
            st.pop();
        }
        reverse(result.begin(), result.end());
        return result;
    }
};
posted @ 2025-07-08 11:02  skyler886  阅读(5)  评论(0)    收藏  举报