力扣 字符串解码

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

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

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;}};
相关推荐
liliangcsdn7 分钟前
多空价差收益序列汇总统计的分析和代码示例
人工智能·算法·机器学习
啥都想学点的研究生14 分钟前
一篇文章讲清楚:Adaboost算法与GBDT
人工智能·算法
旖旎夜光23 分钟前
LeetCode 525:连续数组(前缀和) —— 题解
数据结构·c++·算法·leetcode·前缀和
zander25840 分钟前
LeetCode 32. 最长有效括号
算法
lvwangshu1 小时前
AC 自动机
算法·字符串
shehuiyuelaiyuehao10 小时前
算法31,前缀和,可被k整除的子数组
数据结构·python·算法
测试199811 小时前
如何用appium搭建Android自动化测试框架?
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
203号居民12 小时前
LeetCode hot 100 — 141. 环形链表2
算法·leetcode·链表
玖玥拾12 小时前
LeetCode 202 快乐数
算法·leetcode
LuminousCPP12 小时前
数据结构-二叉树(六):BFS层序遍历与完全二叉树判断|复用链式队列 + (N_0=N_2+1) 性质证明
c语言·数据结构·笔记·算法·二叉树·宽度优先