代码随想录训练营第十天|string天然栈

232.用栈实现队列

class MyQueue {
private:
    stack<int> in_;
    stack<int> out_;


public:
    MyQueue() {

    }
    
    void push(int x) {
        in_.push(x);
    }
    
    int pop() {
        if(out_.empty()){
            while(!in_.empty()){
                out_.push(in_.top());
                in_.pop();
            }
        }
        int val=out_.top();
        out_.pop();
        return val;
    }
    
    int peek() {
        int val=pop();
        out_.push(val);
        return val;
    }
    
    bool empty() {
        return in_.empty()&&out_.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 {
private:
    queue<int> q_;

public:
    MyStack() {
    }
    
    void push(int x) {
        q_.push(x);
    }
    
    int pop() {
        for(int i=0;i<q_.size()-1;i++){
            q_.push(q_.front());
            q_.pop();
        }
        int val=q_.front();
        q_.pop();
        return val;
    }
    
    int top() {
        int val=pop();
        push(val);
        return val;
    }
    
    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> st;
        for(auto c:s){
            switch(c){
                case '(':
                case '[':
                case '{':
                    st.push(c);
                    break;
                case ')':
                    if(!st.empty()&&st.top()=='('){
                        st.pop();
                        break;
                    }
                    else
                        return false;
                case ']':
                    if(!st.empty()&&st.top()=='['){
                        st.pop();
                        break;
                    }
                    else
                        return false;
                case '}':
                    if(!st.empty()&&st.top()=='{'){
                        st.pop();
                        break;
                    }
                    else
                        return false;
            }
            
        }
        return st.empty();
    }
};

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

class Solution {
public:
    string removeDuplicates(string s) {
        string res;
        for(int i=0;i<s.size();i++){
            if(!res.empty()&&s[i]==res.back()){
                res.pop_back();
            }
            else
                res.push_back(s[i]);
        }
        return res;
    }
};

string提供push_back和pop_back接口,天然栈。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值