力扣 字符串解码

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

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

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;}};
相关推荐
XH华1 小时前
备战蓝桥杯,第七章:函数与递归
职场和发展·蓝桥杯
吴维炜1 小时前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
Σίσυφος19002 小时前
PCL Point-to-Point ICP详解
人工智能·算法
玄〤3 小时前
Java 大数据量输入输出优化方案详解:从 Scanner 到手写快读(含漫画解析)
java·开发语言·笔记·算法
weixin_395448913 小时前
main.c_cursor_0202
前端·网络·算法
senijusene3 小时前
数据结构与算法:队列与树形结构详细总结
开发语言·数据结构·算法
杜家老五3 小时前
综合实力与专业服务深度解析 2026北京网站制作公司六大优选
数据结构·算法·线性回归·启发式算法·模拟退火算法
2301_765703144 小时前
C++与自动驾驶系统
开发语言·c++·算法
Ll13045252984 小时前
Leetcode二叉树 part1
b树·算法·leetcode
鹿角片ljp4 小时前
力扣9.回文数-转字符双指针和反转数字
java·数据结构·算法