算法-栈操作

 

1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)

class Solution {
public:
    string removeDuplicates(string s) {
        string stack;
        for(char& ch:s){
            if(stack.size()>0&&ch==stack.back()){
                stack.pop_back();
            }else{
                stack.push_back(ch);
            }
        }
        return stack;
    }
};

844. 比较含退格的字符串 - 力扣(LeetCode)

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        string tmp_s;
        string tmp_t;
        for(char& ch:s){
            if(ch=='#'){
                if(tmp_s.size()>0)
                tmp_s.pop_back();
            }else{
                tmp_s.push_back(ch);
            }
        }
        for(char& ch:t){
            if(ch=='#'){
                if(tmp_t.size()>0)
                tmp_t.pop_back();
            }else{
               tmp_t.push_back(ch);
            }
        }
        if(tmp_s==tmp_t) 
            return true;
        return false;
    }
};

227. 基本计算器 II - 力扣(LeetCode)

class Solution {
public:
    int calculate(string s) {
        vector<int> stack;//栈内不存在加减乘除符号,符号遍历时用op保存,如果有括号的话,需要另一个栈,然后加减符号可以用正负代替插入。
        int i=0; int n=s.size();
        char op='+';
        while(i<n){
            if(s[i]==' ') i++;
            else if(0+'0'<=s[i]&&s[i]<=0+'9'){
                //提取数字
                int tmp=s[i++]-'0';
                while(0+'0'<=s[i]&&s[i]<=0+'9') tmp+=tmp*10+s[i++]-'0';
                cout<<"op: "<<op<<endl;
                cout<<tmp<<endl;
                if(op=='+') stack.push_back(tmp);
                else if(op=='-') stack.push_back(-tmp);
                else if(op=='*'){
                    int tmp1=stack.back();//必须在push前先pop
                    stack.pop_back();
                    stack.push_back(tmp1*tmp);
                    // stack.pop_back();
                }else if(op=='/'){
                    int tmp1=stack.back();
                    stack.pop_back();
                    stack.push_back(tmp1/tmp);
                } 
                for(int num:stack){cout<<num<<" ";}cout<<endl;
            }else {
                // cout<<"op: "<<op<<endl;
                op=s[i++];
            }
        }
        

        int ret=0;
        for(int& num:stack){
            // cout<<num<<endl;
            ret+=num;
        }
        return ret;
    }
};

946. 验证栈序列 - 力扣(LeetCode)

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped){
        stack<int> st;
        int i=0; int n=popped.size();
        for(int &val : pushed){
            st.push(val);
            while(st.size()&&st.top()==popped[i]){
                st.pop();
                i++;
            }
        }
        return st.size()==0;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值