力扣 字符串解码

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

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

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;}};
相关推荐
ʚ希希ɞ ྀ14 分钟前
岛屿数量 -- 图论
算法·深度优先·图论
aWty_1 小时前
实分析入门(11)--Cantor三分集
学习·数学·算法·实变函数
兰令水1 小时前
leecodecode【二叉树递归+对称】【2026.6.1打卡-java版本】
算法
地平线开发者9 小时前
profiler debug 工具用法与高一致性策略
算法·自动驾驶
编程大师哥9 小时前
匿名函数 lambda + 高阶函数
java·python·算法
我叫袁小陌10 小时前
算法解题思路指南
算法
地平线开发者10 小时前
Conv+BN+Add+ReLU 融合机制简介
算法·自动驾驶
yuanyuan2o210 小时前
模型预训练:Hugging Face Transformers 基础
算法·ai·语言模型·自然语言处理·nlp·深度优先
杨充10 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法