力扣 字符串解码

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

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

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;}};
相关推荐
文丑颜不良啊23 分钟前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊2 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊3 小时前
LeetCode2521~2540题目解析
java·leetcode
lisw053 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
一起学安全技术4 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs5 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信150413057596 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
文丑颜不良啊6 小时前
LeetCode2501~2520题目解析
java·leetcode
lsc_blog11 小时前
年终自评别贴提交记录,把技术产出讲成变化
数据库·面试·职场和发展·跳槽·求职招聘