力扣 字符串解码

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

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

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;}};
相关推荐
To_OC2 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
旖-旎3 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC3 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
delishcomcn4 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹4 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术5 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
CV-Climber7 小时前
检索技术的实际应用
人工智能·算法
hhzz8 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_9 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法