力扣 字符串解码

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

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

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;}};
相关推荐
星火开发设计4 分钟前
枚举类 enum class:强类型枚举的优势
linux·开发语言·c++·学习·算法·知识
嘴贱欠吻!5 小时前
Flutter鸿蒙开发指南(七):轮播图搜索框和导航栏
算法·flutter·图搜索算法
张祥6422889046 小时前
误差理论与测量平差基础笔记十
笔记·算法·机器学习
踩坑记录6 小时前
leetcode hot100 2.两数相加 链表 medium
leetcode·链表
qq_192779876 小时前
C++模块化编程指南
开发语言·c++·算法
cici158748 小时前
大规模MIMO系统中Alamouti预编码的QPSK复用性能MATLAB仿真
算法·matlab·预编码算法
历程里程碑8 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
2501_940315269 小时前
航电oj:首字母变大写
开发语言·c++·算法
CodeByV10 小时前
【算法题】多源BFS
算法
TracyCoder12310 小时前
LeetCode Hot100(18/100)——160. 相交链表
算法·leetcode