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

LeetCode232. 用栈实现队列

题目链接:232. 用栈实现队列 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:栈的基本操作! | LeetCode:232.用栈实现队列_哔哩哔哩_bilibili

思路:用栈模拟了队列的几种操作,其中比较有难度的是pop(),这个需要用到两个栈,将内容保存到stIn中,而pop的时候需要把stIn中的全部放入stOut中,再pop。

代码:

class MyQueue {
public:
    stack<int>stIn;
    stack<int>stOut;
    MyQueue() {
        
    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if(stOut.empty()){
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result=stOut.top();
        stOut.pop();
        return result;
    }
    
    int peek() {
        int result=this->pop();
        stOut.push(result);
        return result;
    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

LeetCode225. 用队列实现栈

题目链接:225. 用队列实现栈 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:队列的基本操作! | LeetCode:225. 用队列实现栈_哔哩哔哩_bilibili

思路:这个不同于前一道题,这次用一个队列就可以模拟栈,pop操作其实就是把前size-1个数字先取出来再加入到队尾,这时队头的元素就是原先队尾的元素了。top操作除了需要把最后pop的再加入到队尾,和pop其实是一样的。

代码: 

class MyStack {
public:
    queue<int>que;
    MyStack() {
        
    }
    
    void push(int x) {
        que.push(x);
    }
    
    int pop() {
        int size=que.size();
        size--;
        while(size--){
            que.push(que.front());
            que.pop();
        }
        int result=que.front();
        que.pop();
        return result;
    }
    
    int top() {
        int size=que.size();
        size--;
        while(size--){
            que.push(que.front());
            que.pop();
        }
        int result=que.front();
        que.push(que.front());
        que.pop();
        return result;
    }
    
    bool empty() {
        return que.empty();
    }
};

LeetCode20. 有效的括号

题目链接:20. 有效的括号 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:栈的拿手好戏!| LeetCode:20. 有效的括号_哔哩哔哩_bilibili

思路:这道题的思路是遇到左括号就把对应的右括号放到栈中,在之后遇到右括号的时候就看现在栈顶的是否与其匹配。这题有三种失败情况,分别是左括号多,右括号多和左右括号不匹配,对应到栈中的样子分别是,栈在循环结束后还有若干个右括号剩余,循环还没结束栈内就为空了,右括号和栈顶的右括号型号不一样。

代码:

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

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

题目链接:1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项_哔哩哔哩_bilibili

思路:我的思路是将字符串中的元素加入到栈中,然后判断栈顶的字符是否与当前字符串遍历的字符相同,如果相同直接栈顶pop,如果不同就把当前字符加入到栈中。输出的时候,需要将结果保存到另一个字符串中,然后再reverse一下。

代码:

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for (int i = 0; i < s.size(); i++) {
            if (st.empty()) {
                st.push(s[i]);
            } else if (st.top() != s[i]) {
                st.push(s[i]);
            } else {
                st.pop();
            }
        }
        
        string ss;
        while (!st.empty()) {
            ss += st.top();
            st.pop();
        }
        reverse(ss.begin(), ss.end());
        return ss;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值