力扣 字符串解码

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

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

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;}};
相关推荐
历程里程碑3 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
YGGP4 小时前
【Golang】LeetCode 128. 最长连续序列
leetcode
你撅嘴真丑10 小时前
第九章-数字三角形
算法
uesowys10 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder10 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮10 小时前
AI 视觉连载1:像素
算法
智驱力人工智能11 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥12 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风12 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風12 小时前
8.1 PFH&&FPFH
图像处理·算法