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

232.用栈实现队列

力扣题目链接

栈的基本操作! | LeetCode:232.用栈实现队列

令两个栈,一个输入栈,一个输出栈。

代码

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()!=1)
            {
                stout.push(stin.top());
                stin.pop();
            }
        }
            int result=stout.top();
            stout.pop();
            return result;
    }
    
    int peek() {
        int res=this->pop();
        stout.push(res);
        return res;
    }
    
    bool empty() {
        return stin.empty()&&stout.empty();
    }
};

225. 用队列实现栈

力扣题目链接

​​​​​​队列的基本操作! | LeetCode:225. 用队列实现栈

一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外)

重新添加到队列尾部,此时再去弹出元素就是栈的顺序了,也就是size-1次。

代码

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 res=this->pop();
        que.push(res);
        return res;
    }
    
    bool empty() {
        return que.empty();
    }
};

20. 有效的括号

力扣题目链接

​​​​​​栈的拿手好戏!| LeetCode:20. 有效的括号

这里有三种不匹配的情况:

1.字符串里左方向的括号多余了 ,所以不匹配。

2.括号没有多余,但是括号的类型没有匹配上。

3.字符串里右方向的括号多余了,所以不匹配。

小技巧:

在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多。

代码

class Solution {
public:
    bool isValid(string s) {
        if(s.size()%2!=0)
        return false;
        stack <int> 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();
                            
        }
        return st.empty();
    }
};

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

力扣题目链接

​​​​​​栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项

代码1

使用栈

因为从栈里弹出的元素是倒序的,所以需要再对字符串进行反转一下。

class Solution {
public:
    string removeDuplicates(string S) {
        stack<char> st;
        for (char s : S) {
            if (st.empty() || s != st.top()) {
                st.push(s);
            } else {
                st.pop(); // s 与 st.top()相等的情况
            }
        }
        string result = "";
        while (!st.empty()) { // 将栈中元素放到result字符串汇总
            result += st.top();
            st.pop();
        }
        reverse (result.begin(), result.end()); // 此时字符串需要反转一下
        return result;

    }
};

代码2

push_back(),pop_back()都是从字符串的后面操作,可以类比栈的操作。

拿字符串直接作为栈,这样省去了栈还要转为字符串的操作。

class Solution {
public:
    string removeDuplicates(string s) {
        string result;
        for(int i=0;i<s.size();i++)
        {
            if(result.empty()||result.back()!=s[i])
                result.push_back(s[i]);
            else
                result.pop_back();
        }
        return result;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值