【C++算法】栈

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

  • 题目链接

删除字符中的所有相邻重复项https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution 
{
public:
    string removeDuplicates(string s) 
    {
        string ret;
        for(int i = 0; i < s.size(); i++)
        {
            if(!ret.empty() && ret.back() == s[i])
            {
                ret.pop_back();
            }
            else
            {
                ret += s[i];
            }
        }

        return ret;
    }
};

比较含退格的字符串

  • 题目链接

比较含退格的字符串https://leetcode.cn/problems/backspace-string-compare/description/

  • 算法原理

解法:用栈思想模拟即可

  • 代码展示
cpp 复制代码
class Solution 
{
public:
    bool backspaceCompare(string s, string t) 
    {
        return comString(s) == comString(t);
    }

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

基本计算器

  • 题目链接

基本计算器https://leetcode.cn/problems/basic-calculator-ii/description/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution 
{
public:
    int calculate(string s) 
    {
        stack<int> st;
        char op = '+';
        int i = 0, n = s.size();
        while(i < n)
        {
            if(s[i] == ' ')
            {
                i++;
            }
            else if(i < n && s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while(i < n && s[i] >= '0' && s[i] <= '9')
                {
                    tmp *= 10;
                    tmp += s[i++] - '0';
                }
                 if(op == '+')
                {
                    // 直接入栈
                    st.push(tmp);
                }
                else if(op == '-')
                {
                    // -tmp入栈
                    st.push(-tmp);
                }
                else if(op == '*')
                {
                    // tmp乘栈顶元素,并放在栈顶
                    st.top() *= tmp;
                }
                else
                {
                    // 栈顶元素除tmp,并放在栈顶
                    st.top() /= tmp;
                }
            }
            else
            {
                op = s[i++];
            }
        }
        int ret = 0;
        while(st.size())
        {
            ret += st.top();
            st.pop();
        }

        return ret;
    }
};

字符串解码

  • 题目链接

字符串解码https://leetcode.cn/problems/decode-string/description/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution 
{
public:
    string decodeString(string s) 
    {
        stack<string> str;
        stack<int> num;
        str.push("");
        int i = 0, n = s.size();
        while(i < n)
        {
            if(i < n && s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while(i < n && s[i] >= '0' && s[i] <= '9')
                {
                    tmp *= 10;
                    tmp += s[i++] - '0';
                }
                num.push(tmp);
            }
            else if(i < n && s[i] == '[')
            {
                i++;
                string tmp;
                while(i < n && s[i] >= 'a' && s[i] <= 'z')
                {
                    tmp += s[i++];
                }
                str.push(tmp);
            }
            else if(i < n && s[i] == ']')
            {
                i++;
                int tmpNum = num.top();
                num.pop();
                string tmpString = str.top();
                while(--tmpNum)
                {
                    str.top() += tmpString;
                }
                tmpString = str.top();
                str.pop();
                str.top() += tmpString;
            }
            else
            {
                str.top() += s[i++];
            }
        }

        return str.top();

    }
};

验证栈序列

  • 题目链接

验证栈序列https://leetcode.cn/problems/validate-stack-sequences/description/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution 
{
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) 
    {
        stack<int> ret;
        int i = 0, n = popped.size();
        for(auto x : pushed)
        {
            ret.push(x);
            while(i < n && ret.size() && popped[i] == ret.top())
            {
                ret.pop();
                i++;
            }
        }

        return i == n;
    }
};
相关推荐
技术砖家--Felix36 分钟前
Spring Boot入门篇:快速搭建你的第一个Spring Boot应用
java·开发语言·音视频
国服第二切图仔40 分钟前
Rust开发之使用Trait对象实现多态
开发语言·算法·rust
Yolo566Q43 分钟前
Python驱动的无人机生态三维建模与碳储/生物量/LULC估算全流程实战技术
开发语言·python·无人机
我不是程序猿儿1 小时前
【C#】XtraMessageBox(DevExpress)与MessageBox(WinForms 标准库)的区别
开发语言·c#
电鱼智能的电小鱼1 小时前
基于电鱼 ARM 工控机的井下AI故障诊断方案——让煤矿远程监控更智能、更精准
网络·arm开发·人工智能·算法·边缘计算
玖笙&1 小时前
✨WPF编程进阶【6.1】:图形原则(附源码)
c++·c#·wpf·visual studio
s砚山s2 小时前
代码随想录刷题——二叉树篇(一)
c++·算法·leetcode
含目的基因的质粒2 小时前
Python异常、模块、包
服务器·开发语言·python
AC是你的谎言2 小时前
HTTP和HTTPS
linux·网络·c++·网络协议·学习·http·https
千码君20163 小时前
Go语言:解决 “package xxx is not in std”的思路
开发语言·后端·golang