【LeetCode热题100(69/100)】字符串解码

题目地址: 链接

思路: 栈。

  1. 遇到非]字符,压入栈
  2. 遇到]字符,将[ ]中的字符str拿出来,[前面的数字num拿出来,根据重复numstr字符,并将其压入栈。
ts 复制代码
function decodeString(s: string): string {
    let stk = [];
    for(let str of s) {
        if(str !== ']') stk.push(str);
        else {
            let str = ''
            while(stk[stk.length - 1] !== '[') {
                str = stk.pop() + str;
            }
            stk.pop(); // 弹出 [
            
            let numStr = '';
            while('0' <= stk[stk.length - 1] && stk[stk.length - 1] <= '9') {
                numStr = stk.pop() + numStr;
            }
            let substr = '';
            for(let i = 0; i < parseInt(numStr); i ++) {
                substr += str;
            }
            stk.push(substr);
        }
    }
    return stk.join('');
};
相关推荐
小鹏linux10 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
在角落发呆11 小时前
Linux转发配置:解锁网络互联的核心密码
linux·运维·网络
心中有国也有家11 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
齐潇宇11 小时前
Zabbix 7 概述与配置
linux·zabbix·监控告警
绝知此事11 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院11 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
csdn_aspnet12 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
江公望12 小时前
Ubuntu htop命令,10分钟讲清楚
linux·服务器
哎呦,帅小伙哦12 小时前
Linux 时间:从原子钟到 clock_gettime 的每一面
linux·运维·服务器