力扣 字符串解码

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

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

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;}};
相关推荐
刀法如飞1 小时前
Python列表去重:从新手三连到高阶特技,20种解法全收录
python·算法·编程语言
minji...1 小时前
算法题 动态规划
算法·动态规划
Mr.Rice.Fool1 小时前
rust面试经验1
后端·面试·职场和发展·rust
水蓝烟雨2 小时前
3337. 字符串转换后的长度 II
算法·leetcode
MegaDataFlowers2 小时前
SiliconCompiler workflow
算法
_日拱一卒2 小时前
LeetCode:226翻转二叉树
数据结构·算法·leetcode
踩坑记录2 小时前
leetcode hot100 64. 最小路径和 medium 递归优化
leetcode·深度优先
BirdenT2 小时前
20260424紫题训练
c++·算法
还是阿落呀2 小时前
基本控制结构
开发语言·c++·算法
禧西3 小时前
面试准备——agent和大模型
面试·职场和发展