力扣 字符串解码

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

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

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;}};
相关推荐
INGNIGHT8 小时前
815 · 课程表IV(dfs)
算法·深度优先
文丑颜不良啊9 小时前
LeetCode1~50题目解析
leetcode
Pniubi9 小时前
力扣146.LRU缓存机制
算法·leetcode·缓存
云上工程笔记9 小时前
2026年7B到67B模型训练与推理GPU配置评测:LoRA、量化与全量微调对比
人工智能·算法·机器学习
softlgh9 小时前
鼾声识别:嵌入式离线鼾声检测 SDK 与睡眠健康数据
算法·智能家居·智能音箱·鼾声识别·智能枕头·打呼噜
了了9 小时前
图搜索基础学习笔记
算法
淡海水9 小时前
05-02-源码-Boehm GC标记阶段
算法·unity·c#·游戏引擎·gc
Code_Solitude10 小时前
传智杯备赛代码整理(前4题)
数据结构·算法
Jasmine_llq11 小时前
《P8816 [CSP-J 2022] 上升点列》
数据结构·算法·贪心·排序·二维动态规划 dp·类背包状态转移
hetao173383711 小时前
2026-10-19 hetao1733837 的刷题记录
c++·算法