代码随想录算法训练营第十天|LeetCode.232用栈实现队列 LeetCode.225用队列实现栈 LeetCode20. 有效的括号 LeetCode1047. 删除字符串中的所有相邻重复项

系列文章目录

代码随想录算法训练营|LeetCode.232用栈实现队列 LeetCode.225用队列实现栈 LeetCode20. 有效的括号


`


前言

代码随想录算法训练营Day10|LeetCode.232用栈实现队列 LeetCode.225用队列实现栈 LeetCode20. 有效的括号 LeetCode1047. 删除字符串中的所有相邻重复项
讲解链接
前几天在上集训课,打卡间断了,正在补卡o(╥﹏╥)o
栈(stack)是先入后出的数据结构,
栈类似于一个整理箱,我们往整理箱里面放一层一层衣服,如果我们想取出底下的衣服,就要先把它上方的所有衣服都拿出来,才能取出这件衣服


一、LeetCode.232用栈实现队列

1、题目链接

LeetCode.232用栈实现队列

2 、思路

用栈实现队列需要两个栈,分别表示入队和出队,入队栈为stackIn,出队栈stackOut
(1)push:向stackIn加入x
(2)pop:
1)若出队栈不为空,则弹出出队栈栈顶元素
2)若出队栈为空,则将输入栈全部移到出队栈,弹出出队栈栈顶
(3)peek:利用pop,把弹出的元素压回去
(4)empty:入栈和出栈同时为空则返回true

3、题解

class MyQueue {
public:
    stack<int> stackIn;
    stack<int> stackOut;
    MyQueue() {

    }
    
    void push(int x) {
        stackIn.push(x);
    }
    
    int pop() {
        
        if(stackOut.empty())
        {
            while(!stackIn.empty())
            {
                stackOut.push(stackIn.top());
                stackIn.pop();
            }
        }
        int res=stackOut.top();
        stackOut.pop();
        return res;
    }
    
    int peek() {
    int res = this->pop(); // 直接使用已有的pop函数
        stackOut.push(res); 
        return res;
       
    }
    
    bool empty() {
        if(stackOut.empty()&&stackIn.empty())return 1;
        else return 0;
    }
};

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

二、LeetCode.225用队列实现栈

1、题目链接

LeetCode.225用队列实现栈

2、思路

用队列实现栈需要两个队列
(1)push:向q1加入x
(2)pop:
栈顶元素为最后一个放入的元素,将前n-1个元素转移到q2中,弹出剩下的那个元素后,将
1)若出队栈不为空,则弹出出队栈栈顶元素
2)若出队栈为空,则将输入栈全部移到出队栈,弹出出队栈栈顶
(3)peek:与pop思路一样,把弹出的元素压回q1
(4)empty:q1为空则返回true

3、题解

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

    }
    
    void push(int x) {
        q1.push(x);
    }
    
    int pop() {
        int n=q1.size();
        for(int i=0;i<n-1;i++)
        {
            q2.push(q1.front());
            q1.pop();
        }
        int res= q1.front();
        q1.pop();
        for(int i=0;i<n-1;i++)
        {
            q1.push(q2.front());
            q2.pop();
        }
        return res;
    }
    
    int top() {
            int n=q1.size();
        for(int i=0;i<n-1;i++)
        {
            q2.push(q1.front());
            q1.pop();
        }
        int res= q1.front();
        q1.pop();

        for(int i=0;i<n-1;i++)
        {
           q1.push(q2.front());
            q2.pop();
        }
        q1.push(res);
        return res;
        
    }
    
    bool empty() {
        return q1.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();
 */

三、LeetCode20. 有效的括号

1、题目链接

LeetCode20. 有效的括号

2、思路

(1)将遇到的“(”,“[”,“{”都放入栈中
(2)遇到“)”,“]”,“}”:
1)栈为空:说明没有对应的左括号进行匹配
2)栈非空
遇到“)”时栈顶若不是“(”,那么没有匹配,返回false
遇到“)”时栈顶若是“(”,返回true
遇到“]”时栈顶若不是“[”,那么没有匹配,返回false
遇到“]”时栈顶若是“[”,返回true
遇到“}”时栈顶若不是“{”,那么没有匹配,返回false
遇到“}”时栈顶若是“{”,返回true

3、题解

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

四、 LeetCode1047. 删除字符串中的所有相邻重复项

1、题目链接

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

2、思路

遍历字符串
如果栈顶元素和现在的元素不相等,入栈;相等则弹出栈顶元素
返回字符串时,注意从栈中弹出的字符串是倒序

3、题解

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> s1;
        stack<char> s2;
        string res;
        int n = s.size();
        for (int i = 0; i < n; i++) {
            if (s1.empty()) {
                s1.push(s[i]);
            } else {
                if (s1.top() != s[i]) {
                    s1.push(s[i]);
                } else {
                    s1.pop();
                }
            }
        }
        int m = s1.size();
        for (int i = 0; i < m; i++) {
            s2.push(s1.top());
            s1.pop();
        }
        for (int i = 0; i < m; i++) {
            res += s2.top();
            s2.pop();
        }
        return res;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值