力扣 字符串解码

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

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

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;}};
相关推荐
Dlrb12114 小时前
C语言-指针三
c语言·算法·指针·const·命令行参数
Tisfy5 小时前
LeetCode 2540.最小公共值:双指针(O(m+n))
算法·leetcode·题解·双指针
IronMurphy5 小时前
【算法四十七】152. 乘积最大子数组
算法
淘矿人6 小时前
Claude辅助DevOps实践
java·大数据·运维·人工智能·算法·bug·devops
Cosolar6 小时前
万字详解:RAG 向量索引算法与向量数据库架构及实战
数据库·人工智能·算法·数据库架构·milvus
小江的记录本6 小时前
【Java基础】泛型:泛型擦除、通配符、上下界限定(附《思维导图》+《面试高频考点清单》)
java·数据结构·后端·mysql·spring·面试·职场和发展
ychqsq6 小时前
20.面试
经验分享·职场和发展
凯瑟琳.奥古斯特7 小时前
数据冗余与规范化的本质[数据库原理]
开发语言·数据库·职场和发展
落羽的落羽7 小时前
【算法札记】练习 | Week4
linux·服务器·数据结构·c++·人工智能·算法·动态规划