day11【c++】【栈与队列转换】● 232.用栈实现队列 ● 225. 用队列实现栈 ● 20. 有效的括号 ● 1047. 删除字符串中的所有相邻重复项

232 用栈实现队列

在这里插入图片描述

思路

在push数据的时候,只要数据放进输入栈就好,但在pop的时候,操作就复杂一些,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入),再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    /** Initialize your data structure here. */
    MyQueue() {

    }
    /** Push element x to the back of queue. */
    void push(int x) {
        stIn.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    //pop()队列的出栈,要先放进stOut,再出去
    int pop() {
        // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
        if (stOut.empty()) {
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()) {
                stOut.push(stIn.top());  //top()是获取元素
                stIn.pop();  //再把栈的元素都弹出
            }
        }
        int result = stOut.top();  //用result来收集
        stOut.pop();  //再把栈的元素都弹出
        return result;
    }

    /** Get the front element. */
    int peek() {
        int res = this->pop(); // 直接使用已有的pop函数
        stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
        return res;
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

225. 用队列实现栈(让先进先出的队列实现先进后出)

让在队列前面的元素再压回队列的尾部,弹出后进去的那个元素,将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部

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());// 将队列头部的size-1个元素重新添加到队列尾部
            que.pop();
        }
        int result = que.front();//返回队列的第一个元素值
        que.pop();
        return result;
    }
    
    int top() {
        return que.back();
    }
    
    bool empty() {
        return que.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();
 */

20. 有效的括号

在这里插入图片描述

题意

给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串,判断字符串是否有效。

什么叫有效呢?"()“或者”()[]{}“或者”{[]}"都是true。
"([)]"就是无效。

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        if(s.size()%2 != 0) return false;  //剪枝:先把简单的不符合情况的去掉
        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. 删除字符串中的所有相邻重复项

思路

我们在删除相邻重复项的时候,其实就是要知道当前遍历的这个元素,我们在前一位是不是遍历过一样数值的元素,那么如何记录前面遍历过的元素呢?

所以就是用栈来存放,那么栈的目的,就是存放遍历过的元素,当遍历当前的这个元素的时候,去栈里看一下我们是不是遍历过相同数值的相邻元素。

方法一:使用栈

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值