代码随想录day10|232

文章链接:

代码随想录代码随想录PDF,代码随想录网站,代码随想录百度网盘,代码随想录知识星球,代码随想录八股文PDF,代码随想录刷题路线,代码随想录知识星球八股文icon-default.png?t=N7T8https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE232

class MyQueue {
private:
    std::stack<int> s1, s2;

public:
    MyQueue() {}

    /**
     * 添加元素到队尾
     */
    void push(int x) { s1.push(x); }

    /**
     * 删除队头的元素并返回
     */
    int pop() {
        // 先调用 peek 保证 s2 非空
        peek();
        int top = s2.top();
        s2.pop();
        return top;
    }

    /**
     * 返回队头元素
     */
    int peek() {
        if (s2.empty()) {
            // 把 s1 元素压入 s2
            while (!s1.empty()) {
                s2.push(s1.top());
                s1.pop();
            }
        }
        return s2.top();
    }

    /**
     * 判断队列是否为空
     */
    bool empty() { return s1.empty() && s2.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 {
    queue<int>q;
    int top_elem = 0;
public:
    MyStack() {
        
    }
    
    void push(int x) {
        q.push(x);
        top_elem = x;
    }
    
    int pop() {
        int size = q.size();
        while(size >2){
            q.push(q.front());
            q.pop();
            size--;
        }
        top_elem = q.front();
        q.push(q.front());
        q.pop();

        int tmp = q.front();
        q.pop();
        return tmp;
    }
    
    int top() {
        return top_elem;
    }
    
    bool empty() {
        return q.empty();
    }
};

20

class Solution {
public:
    bool isValid(string s) {
        stack<char>left;
        for(char c:s){
            if(c == '(' || c== '{' || c == '[')
                left.push(c);
            else
                if(!left.empty() && leftOf(c)== left.top())
                    left.pop();
                else
                    return false;
        }
        return left.empty();
    }
private:
    char leftOf(char c){
        if(c == '}') return '{';
        if(c == ')') return '(';
        return '[';
    }
};

1047

class Solution {
public:
    string removeDuplicates(string s) {
        string temp = "";
        int i = 0;

        while(i <s.length()){
            if(temp.empty() || s[i]!=temp.back()){
                 temp.push_back(s[i]);
            }else{
                temp.pop_back();
            }
            i++;      
        }
        return temp;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值