leetcode算法之栈

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

删除字符串中的所有相邻重复项
在这里插入图片描述

class Solution {
public:
    string removeDuplicates(string s) {
        string ret;//使用数组模拟栈操作
        for(auto ch:s)
        {
            if(ret.size()&& ch == ret.back()) ret.pop_back();
            else ret+=ch;
        }
        return ret;
    }
};

2.比较含退格的字符串

比较含退格的字符串
在这里插入图片描述

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        return changeStr(s) == changeStr(t);
    }

    string changeStr(string& s)
    {
        string ret;
        for(auto ch:s)
        {
            if(ch != '#') ret+=ch;
            else 
            {
                if(ret.size())
                {
                    ret.pop_back();
                }
            }
        }
        return ret;
    }
};

3.基本计算器II

基本计算器II
在这里插入图片描述

class Solution {
public:
    int calculate(string s) {
        //双栈
        vector<int> st;//使用数组来模拟栈结构
        char op = '+';//使用变量来模拟栈结构
        int i = 0,n = s.size();
        while(i<n)
        {
            if(s[i] ==' ') 
            {
                i++;
            }
            else if(s[i]>='0'&&s[i]<='9')
            {
                int tmp = 0;
                while(i<n && (s[i]>='0'&&s[i]<='9')) tmp = tmp*10+(s[i++]-'0');
                if(op == '+') st.push_back(tmp);
                else if(op == '-') st.push_back(-tmp);
                else if(op == '*') st.back()*=tmp;
                else if(op == '/') st.back() /= tmp;
            }
            else
            {
                op = s[i];
                i++;
            }
        }
        int ret = 0;
        for(auto x:st) ret+=x;
        return ret;
    }
};

4.字符串解码

字符串解码
在这里插入图片描述

class Solution {
public:
    string decodeString(string s) {
        //双栈
        stack<string> st;
        stack<int> nums;
        st.push("");
        int i =0,n = s.size();
        while(i<n)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                int tmp = 0;
                while(s[i]>='0'&&s[i]<='9') tmp = tmp*10+(s[i++]-'0');
                nums.push(tmp);
            }
            else if(s[i] == '[')
            {
                i++;
                string tmp;
                while(s[i]>='a'&&s[i]<='z') tmp+=s[i++];
                st.push(tmp);
            }
            else if(s[i] == ']')
            {
                string tmp = st.top();
                st.pop();
                int k = nums.top();
                nums.pop();
                while(k--)
                {
                    st.top()+=tmp;
                }
                i++;
            }
            else
            {
                string tmp;
                while(i<n && s[i]>='a'&&s[i]<='z') tmp+=s[i++];
                st.top()+=tmp;
            }
        }
        return st.top();
    }
};

5.验证栈序列

验证栈序列
在这里插入图片描述

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> st;
        int i =0,n = popped.size();
        for(auto ch:pushed)
        {
            st.push(ch);
            while(st.size() && st.top() == popped[i]) 
            {
                st.pop();
                i++;
            }
        }
        return i==n;
    }
};
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值