当前位置: 首页 > article >正文

leetcode算法之栈

目录

  • 1.删除字符串中的所有相邻重复项
  • 2.比较含退格的字符串
  • 3.基本计算器II
  • 4.字符串解码
  • 5.验证栈序列

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;
    }
};

http://www.kler.cn/a/158137.html

相关文章:

  • YOLOv3 快速上手:Windows 10上的训练环境搭建
  • RHCSA学习笔记(RHEL8) - Part1.RH124
  • 12、SQL注入——SQL报错注入
  • IDEA中,Archetype的作用
  • 【开源】基于JAVA的厦门旅游电子商务预订系统
  • 23史上最全版---SQL注入详解
  • Vue3中props传参(多种数据类型传参方式)
  • 使用python+poco+夜神模拟器进行自动化测试实例
  • 全球葡萄酒行业对社会的积极和消极影响
  • 【Git】Git撤销操作
  • oracle 19c rac 安装手册
  • Golang线程池与协程池
  • 全国网络与信息安全管理员职工职业技能竞赛线下培训—简单的流量分析
  • 欢迎回到 C++ - 现代 C++(心得-壹)
  • Atcoder Beginner Contest 331 A~F
  • Python 文本终端 GUI 框架详解
  • 科技论文中的Assumption、Remark、Property、Lemma、Theorem、Proof含义
  • 论文阅读[2022sigcomm]GSO-Simulcast Global Stream Orchestration in Simulcast Video
  • CocosCreator 面试题(十九) Cocos Creator 材质 shader 分别是什么?
  • 【Vulnhub 靶场】【Prime (2021): 2】【简单 - 中等】【20210509】