力扣 字符串解码

维护一个放数字的栈,一个放字母的栈

遇到[把数字和字母入栈,遇到]把当前字母循环加上数字栈头遍的字母栈头

cpp 复制代码
class Solution {
public:
    string decodeString(string s) {
     string ans="";
     stack<int>sz;
 stack<string>zm;里面是string 还是Char,要看你定义往里加的字符是哪个类型
     int count=0;
     for(auto& x:s){
        if(isdigit(x))
            count=10*count+x-'0';
        else if(x=='['){
            sz.push(count);
            count=0;
            zm.push(ans);
            ans="";     }
        else if(isalpha(x))
        ans+=x;
        else if(x==']'){
            int n=sz.top();
            string cur=zm.top();
            sz.pop();
            zm.pop();
            while(n--){
                cur+=ans;   }
            ans=cur;}    }
     return ans;}};
相关推荐
独断万古他化7 分钟前
【算法通关】位运算:位图、异或消消乐,高频算法题全解
算法·位运算
你的冰西瓜18 分钟前
C++ STL算法——修改序列算法
开发语言·c++·算法·stl
大黄说说21 分钟前
彻底删除重复节点——LeetCode 82 题「有序链表去重 II」详解
算法·leetcode·链表
如意猴29 分钟前
003【高精度算法】加法/减法/乘法/除法
算法
仰泳的熊猫31 分钟前
题目1465:蓝桥杯基础练习VIP-回形取数
数据结构·c++·算法·蓝桥杯
Hag_2033 分钟前
LeetCode Hot100 15.三数之和
算法·leetcode·职场和发展
俩娃妈教编程39 分钟前
洛谷选题:P1307 [NOIP 2011 普及组] 数字反转
c++·算法
laplace01231 小时前
浮点数精度
人工智能·算法·agent·qwen
lczdyx1 小时前
告别付费与隐私泄露!用Pandoc在本地免费、安全地将Markdown转为Word(保姆级教程)
安全·职场和发展·word·办公·自由职业者
blackicexs1 小时前
第四周第五天
数据结构·算法