代码随想录算法训练营Day.10 栈与队列 part01 | 用栈实现队列 用队列实现栈 有效的括号 删除字符串中的所有相邻重复项

48 阅读2分钟

232.用栈实现队列

非常简单的栈实现队列,主要是熟悉一下CPP的类,以及栈的基本操作。当然,基本思想是,栈到另一个栈时,出栈的顺序就会发生反转,从逆序回到正序。

// 两个栈实现队列
class MyQueue {
public:
    std::stack<int> stPush,stPop;
    int size;
    MyQueue() {
        size = 0;
    }
    
    void push(int x) {
        stPush.push(x);
        size ++;
    }
    
    int pop() {
        if(stPop.empty())
        {
            // if(stPush.size() == 0) return -1;
            while(stPush.size() > 0)
            {
                int num = stPush.top();
                stPop.push(num);
                stPush.pop();
            }
        }
        int res = stPop.top();
        stPop.pop();
        size --;
        return res;
    }
    
    int peek() {
        if(stPop.empty())
        {
            while(stPush.size() > 0)
            {
                int num = stPush.top();
                stPop.push(num);
                stPush.pop();
            }
        }
        return stPop.top();
    }
    
    bool empty() {
        if(stPop.empty() && stPush.empty()) return true;
        else 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();
 */

225.用队列实现栈

双队列实现栈,在pop时需要两个队列来回轮换。另外就是top需要多加考虑。

class MyStack {
public:
    std::queue<int> qA,qB;
    bool isA;
    int size;
    MyStack() {
        size = 0;
        isA = true;
    }
    
    void push(int x) {
        if(isA)
            qA.push(x);
        else
            qB.push(x);
        size ++;
    }
    
    int pop() {
        int res{0};
        if(qA.size() >= 1)
        {
            while(qA.size() > 1)
            {
                int num = qA.front();
                qB.push(num);
                qA.pop();
            }
            res = qA.front();
            qA.pop();
            isA = true;
        }
        else
        {
            while(qB.size() > 1)
            {
                int num = qB.front();
                qA.push(num);
                qB.pop();
            }
            res = qB.front();
            qB.pop();
            isA = false;
        }
        size --;
        return res;
    }
    
    int top() {
        if(isA && !qA.empty() || qB.empty()) return qA.back();
        else return qB.back();
    }
    
    bool empty() {
        if(qA.empty() && qB.empty()) return true;
        else 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.有效的括号

做了一些简单的剪枝,然后主要就是用stack实现。

class Solution {
public:
    bool isValid(string s) {
        if(s.size() % 2 != 0) return false;
        std::stack<char> braces;
        for(char c : s)
        {
            if(c == '(' || c == '{' || c == '[')
            {
                braces.push(c);
            }
            else
            {
                if(braces.size() == 0) return false;
                if(c == ')')
                {
                    if(braces.top() == '(')
                        braces.pop();
                    else
                        return false;
                }
                else if(c == '}')
                {
                    if(braces.top() == '{')
                        braces.pop();
                    else
                        return false;
                }
                else
                {
                    if(braces.top() == '[')
                        braces.pop();
                    else
                        return false;
                }
            }
        }
        if(braces.size() > 0) return false;
        return true;
    }
};

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

stack的一个特点就是,可以时刻记录到刚刚插入的值。

class Solution {
public:
    string removeDuplicates(string s) {
        std::stack<char> stackA{};
        for(char c : s)
        {
            if(stackA.size() > 0)
            {
                if(c == stackA.top()) 
                {
                    stackA.pop();
                    continue;
                }
            }
            stackA.push(c);
        }
        std::string res{};
        while(stackA.size() > 0)
        {
            res += stackA.top();
            stackA.pop();
        }
        std::reverse(res.begin(), res.end());
        return res;
    }
};