代码随想录 第五章 栈与队列part01 232.用栈实现队列 225. 用队列实现栈 20. 有效的括号 1047. 删除字符串中的所有相邻重复项

232.用栈实现队列

class MyQueue {
public:
    stack<int> in;
    stack<int> out;
    MyQueue() {

    }
    
    void push(int x) {
        in.push(x);
    }
    
    int pop() {
        if(out.empty()){
            while(!in.empty()){
                out.push(in.top());
                in.pop();
            }
        }
        int result=out.top();
        out.pop();
        return result;
    }
    
    int peek() {
        int result=this->pop();
        out.push(result);
        return result;
    }
    
    bool empty() {
        if(in.empty()&&out.empty()){
            return true;
        }
        return false;
    }
};

/**
 * 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();
 */

为什么pop的部分不在pop之后把元素从out中吐回in呢,乍一想可能有点抽象,因为就算吐回in后下次pop时最后压入out的元素依然是in的底部,也就是吐回in前,out的顶部,所以不把元素搬回in也完全没影响。

225. 用队列实现栈

class MyStack {
public:
    queue<int> q1;
    MyStack() {

    }
    
    void push(int x) {
        q1.push(x);
    }
    
    int pop() {
        queue<int> q2;
        int size=q1.size();
        while(--size){
            q2.push(q1.front());
            q1.pop();
        }
        int result=q1.front();
        q1.pop();
        q1=q2;
        return result;
    }
    
    int top() {
        int result=this->pop();
        q1.push(result);
        return result;
    }
    
    bool empty() {
        if(q1.empty()){
            return true;
        }
        return false;
    }
};

/**
 * 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> kh;
        for(int i=0;i<s.size();i++){
            if(s[i]=='('||s[i]=='{'||s[i]=='['){
                kh.push(s[i]);
            }else{
                if(kh.empty()){
                    return false;
                }
                if(s[i]!=kh.top()+1&&s[i]!=kh.top()+2){
                    return false;
                }
                kh.pop();
            }
        }
        if(kh.empty()){
            return true;
        }
        return false;
    }
};

'('+1=')', '['+2=']', '{'+2='}'。

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

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> result;
        for(int i=s.size()-1;i>=0;i--){
            if(!result.empty()){
                if(s[i]==result.top()){
                    result.pop();
                }else{
                    result.push(s[i]);
                }
            }else{
                result.push(s[i]);
            }
        }
        int i=0;
        while(!result.empty()){
            s[i++]=result.top();
            result.pop();
        }
        s.resize(i);
        return s;
    }
};

将字符串压入堆栈前看栈顶字符是不是和要压入的字符一样,如果一样代表两字符相邻且相等,将栈顶字符弹出,如此往复,直到遍历完字符串即可。

代码随想录 第五章 栈与队列part01

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值