力扣 字符串解码

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

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

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 小时前
LeetCode100天Day7-移动零与搜索插入位置
数据结构·算法·leetcode·指针
ullio7 小时前
div1+2. 2178E - Flatten or Concatenate
算法
yu_anan1117 小时前
PPO/GRPO算法在RLHF中的实现
算法
leoufung7 小时前
Word Break:深度理解 DP 前缀结束点的核心思想
算法·word·动态规划
Aaron15887 小时前
三种主流接收机架构(超外差、零中频、射频直采)对比及发展趋势浅析
c语言·人工智能·算法·fpga开发·架构·硬件架构·信号处理
乐迪信息11 小时前
乐迪信息:目标检测算法+AI摄像机:煤矿全场景识别方案
人工智能·物联网·算法·目标检测·目标跟踪·语音识别
前端小L16 小时前
贪心算法专题(十):维度权衡的艺术——「根据身高重建队列」
javascript·算法·贪心算法
方得一笔16 小时前
自定义常用的字符串函数(strlen,strcpy,strcmp,strcat)
算法
Xの哲學17 小时前
Linux SMP 实现机制深度剖析
linux·服务器·网络·算法·边缘计算
wuk99817 小时前
使用PCA算法进行故障诊断的MATLAB仿真
算法·matlab