【代码随想录_Day9】 232.用栈实现队列 、 225. 用队列实现栈 、 20. 有效的括号 、 1047. 删除字符串中的所有相邻重复项

以下是今日份的总结

232.用栈实现队列
225. 用队列实现栈
20. 有效的括号
1047. 删除字符串中的所有相邻重复项

232 熟练运用stack的pop,push以及top;

225 熟练运用 queue的pop和push以及front和back;

20 运用辅助栈;

1047 string本身有类似的栈操作。

今天的题目全是easy题,但是出现在细枝末节的本体比较多,记了笔记,回看的时候可以避免类似的错误^ _ ^

用栈实现队列

思路:

使用两个栈,分别模拟入队和出队,以此来达到队列先进先出的特性

值得注意的是

stack的栈顶元素是top()

class MyQueue {
public:
    stack<int> stIn;  // 模拟入队用的栈
    stack<int> stOut; // 模拟出队用的栈
    MyQueue() {

    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if(stOut.empty()){
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()){
                stOut.push(stIn.top());//向stOut压入stIn的顶端元素
                stIn.pop();//让stIn顶端元素出栈
            }
        }
        int res = stOut.top();//出队的元素是stOut的顶端元素
        stOut.pop();//出栈
        return res;
    }
    
    int peek() {
        int res = this->pop();//直接使用写好的pop函数
        stOut.push(res);//再把出队的元素压入stOut栈顶
        return res;
    }
    
    bool empty() {
        if(stIn.empty()&&stOut.empty()){//需要同时满足出入栈都为空时,队列才为空
            return true;
        }
        else{
            return false;
        }
    }
};

用队列实现栈

思路:

用一个队列实现栈的出栈和进栈,另外用一个队列记录出队的元素并重新赋值,以此来达到模拟栈的先进后出特性

值得注意的是

queue的队头是front,队尾是back

class MyStack {
public:
    queue<int> q1;
    queue<int> q2; // 辅助备份

    MyStack() {}

    void push(int x) { q1.push(x); }

    int pop() {
        int size = q1.size();
        size--;
        while (size--) {
            q2.push(q1.front());
            q1.pop();
        }
        int res = q1.front();
        q1.pop();
        q1 = q2;
        while (!q2.empty()) {
            q2.pop();
        }
        return res;
    }

    int top() {
        return q1.back();
    }

    bool empty() {
        return q1.empty();
    }
};

有效的括号

思路:

用辅助栈和字符串进行左右匹配,左右括号匹配就弹出并且遍历下一个字符,直至辅助栈中没有元素

值得注意的是

栈的出入以及字符串的遍历,容易出现数组越界的问题

    bool isValid(string s) {
        stack<char> st;
        st.push(' ');
        for(int i = 0;i<s.length();i++){     
            if(st.top()=='{'&&s[i]=='}'||st.top()=='['&&s[i]==']'||st.top()=='('&&s[i]==')'){
                st.pop();
                continue;
            }
            st.push(s[i]); 
        }
        if(st.top()==' '){
            return true;
        }else{
            return false;
        }   
    }

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

思路:

用上一题的方法也可以做出来但是时间会比较久,可以直接运用string提供的类似栈的操作

值得注意的是

string是直接在字符串末尾操作,无序考虑逆序问题

    string removeDuplicates(string s) {
        string stk;
        for (char ch : s) {
            if (!stk.empty() && stk.back() == ch) {
                //当stk的最后一位和当前遍历的字符相等时
                stk.pop_back();//弹出最后一位
            } else {
                stk.push_back(ch);
            }
        }
        return stk;
    }
string removeDuplicates(string s) {
        stack<char>st;
        st.push(' ');
        string ss;
        for(int i = 0;i<s.length();i++){
            if(st.top()==s[i]){
                st.pop();
                continue;
            }
            st.push(s[i]);
        }
        while(st.top()!=' '){
            ss = st.top() + ss;
            st.pop();
        }
        return ss;
}

写在最后

----OK,今日份的博客就写到这里,这一期的题全是基础操作,明天继续加油!!!
—看了看下期的题,你确定难度梯度是合理的吗(我打宿傩?.jpg);
–追上时间进度了吗?如追,还需加油!!(笑
-我今晚就要变成✨✨。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值