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

232.用栈实现队列

class MyQueue {
public:

    stack<int>sin;  //输入栈
    stack<int>sout;   //输出栈

    //构造函数
    MyQueue() {

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

    }
    
    int pop() {
        if(sout.empty())
        {
            while(!sin.empty())
            {
                sout.push(sin.top());
                sin.pop();
            }
        }
        else
        {
            int b=sout.top();
            sout.pop();
            return b;
        }
        
        int a=sout.top();
        sout.pop();
        return a;
    }
    
    //返回队头元素
    int peek() {
        if(sout.empty())
        {
            while(!sin.empty())
            {
                sout.push(sin.top());
                sin.pop();
            }
        }
        else
        {
           return sout.top();
        }
        
        return sout.top();

    }
    
    bool empty() {
        if(sin.empty()&&sout.empty())
        {
            return true;
        }
        else
        {
            return false;
        }

    }
};

225. 用队列实现栈

1. 运行超出时间限制,检查while循环里面有没有写 循环改变的条件。

2. 堆栈  s.top()是获取栈顶元素。  队列 q.front() 是获取队头元素。 

class MyStack {
public:
    //注意:1. 队列是有size操作的,比stack多的一个操作

    queue<int> q;

    MyStack() {

    }
    
    void push(int x) {
        q.push(x);
    }
    
    //移除并返回栈顶元素
    int pop() {
        int t=q.size()-1;
        while(t)
        {
            q.push(q.front());
            q.pop();
            t--; //2. 又忘了写t--,但凡超出时间限制就检查一下while循环是否是死循环。
        }
        int a=q.front(); //3. queue是front ,不是stack的 top
        q.pop();
        return a;
    }
    

    int top() {
        int a=this->pop();//做了出栈操作后,要重新入栈
        q.push(a);
        return a;

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

20. 有效的括号

1. 学习解题思路,我在使用map的时候,把 右括号设为 key,左括号设为了  value,这样方便检索。再进一步,可以直接 右括号 入栈。 较为方便。

2. if(st.empy()||   )这句, 与或 有短路原则, st.empy()判断为1后,后面的不再进行计算。

3. 空栈 空队列不能使用 top(),会报错。

class Solution {
public:
    bool isValid(string s) {

        stack<char> st;
        map<char,char> door;
        door.insert(pair<char,char>(')','('));
        door.insert(pair<char,char>(']','['));
        door.insert(pair<char,char>('}','{'));
        for(int i=0;i<s.size();i++)
        {
            if((s[i]=='(')||(s[i]=='[')||(s[i]=='{'))
            {
                st.push(s[i]);
            }
            else //少考虑了一种情况 ]] ,就是栈直接为空
            {
                //第一种情况:括号对不上 ([})
                //第二种情况:([]) }}  括号多了 
                if(st.empty()||door[s[i]]!=st.top()) return false; //短路原则
                else st.pop();
            }              
            
        }
        return st.empty();  //第三种情况: 多的左括号 [[
    }
};
//卡哥版本
class Solution {
public:
    bool isValid(string s) {
        if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求
        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(']');
            // 第三种情况:遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
            // 第二种情况:遍历字符串匹配的过程中,发现栈里没有我们要匹配的字符。所以return false
            else if (st.empty() || st.top() != s[i]) return false;
            else st.pop(); // st.top() 与 s[i]相等,栈弹出元素
        }
        // 第一种情况:此时我们已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false,否则就return true
        return st.empty();
    }
};

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

1. debug老是通不过的时候,不妨问一下chatgpt。 又一次 把 == 写成了 =。 

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(s[i]==st.top()) st.pop(); //一开始这里又写成了=
            else st.push(s[i]);
        }
        
        //栈里面是剩下的字符串
        string res = "";
        while (!st.empty()) { // 将栈中元素放到result字符串汇总
            res.push_back(st.top());
            // res+= st.top();
            st.pop();
        }

        //将字符串反转
        int start=0;
        int end=res.size()-1;
        //for(int start,end;start<end;start++,end--) 又重新声明了一个 start ,end。有问题
        for(start,end;start<end;start++,end--)
        {
            swap(res[start],res[end]);
        }

        //更简单的方法使用reverse函数
        //reverse(res.begin(),res.end())  对 从0开始 ,到end前面一位 结束
        return res;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值