当前位置:网站首页>LeetCode:394. 字符串解码
LeetCode:394. 字符串解码
2022-07-06 08:44:00 【Bertil】
给定一个经过编码的字符串,返回它解码后的字符串。
编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
示例 1:
输入:s = "3[a]2[bc]"
输出:"aaabcbc"
示例 2:
输入:s = "3[a2[c]]"
输出:"accaccacc"
示例 3:
输入:s = "2[abc]3[cd]ef"
输出:"abcabccdcdcdef"
示例 4:
输入:s = "abc3[cd]xyz"
输出:"abccdcdcdxyz"
提示:
- 1 <= s.length <= 30
- s 由小写英文字母、数字和方括号 ‘[]’ 组成
- s 保证是一个 有效 的输入。
- s 中所有整数的取值范围为 [1, 300]
解题思路
1.由内到外,层层解决[ ],需要保持对字符的记忆,因此用栈
2.入栈时机:遇到 [ 意味着要先解决内部的,外部的数字和字母去栈里等
- 当遇到[,已经扫描的数字就是“倍数”,入栈暂存
- 当遇到[,已经扫描的字母也入栈等待,等括号里的解码完了,就能一起参与构建字符串
3.出栈时机:遇到 ] 说明内层的扫描完了,栈顶元素(最近遇到的“倍数”和字母)可以出栈了,共同参与子串的构建
代码
/** * @param {string} s * @return {string} */
var decodeString = function(s) {
let numStack = []; // 存倍数的栈
let strStack = []; // 存 待拼接的str 的栈
let num = 0; // 临时倍数
let result = ''; // 临时字符串
for (const char of s) {
// 逐字符扫描
if (!isNaN(char)) {
// 遇到数字
num = num * 10 + Number(char); // 算出倍数
} else if (char == '[') {
// 遇到 [
strStack.push(result); // result串入栈
result = '';
numStack.push(num); // 倍数num进入栈等待
num = 0;
} else if (char == ']') {
// 遇到 ],两个栈的栈顶出栈
let repeatTimes = numStack.pop(); // 获取拷贝次数
result = strStack.pop() + result.repeat(repeatTimes); // 构建子串
} else {
result += char; // 遇到字母,追加给result串
}
}
return result;
};
边栏推荐
- Unsupported operation exception
- 2022.02.13 - NC001. Reverse linked list
- 游戏解包的危害及资源加密的重要性
- 软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
- Screenshot in win10 system, win+prtsc save location
- 堆排序详解
- Cisp-pte practice explanation
- Roguelike游戏成破解重灾区,如何破局?
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- [NVIDIA development board] FAQ (updated from time to time)
猜你喜欢

Process of obtaining the electronic version of academic qualifications of xuexin.com

Restful API design specification

企微服务商平台收费接口对接教程

2022.02.13 - NC004. Print number of loops

win10系统中的截图,win+prtSc保存位置

tree树的精准查询

可变长参数

深度剖析C语言指针
![[brush questions] top101 must be brushed in the interview of niuke.com](/img/55/5ca957e65d48e19dbac8043e89e7d9.png)
[brush questions] top101 must be brushed in the interview of niuke.com

【嵌入式】Cortex M4F DSP库
随机推荐
Bottom up - physical layer
vb.net 随窗口改变,缩放控件大小以及保持相对位置
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
JVM performance tuning and practical basic theory - Part 1
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
LeetCode:剑指 Offer 42. 连续子数组的最大和
可变长参数
sublime text中conda环境中plt.show无法弹出显示图片的问题
Simple use of promise in uniapp
The network model established by torch is displayed by torch viz
Purpose of computer F1-F12
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
The mysqlbinlog command uses
被破解毁掉的国产游戏之光
Browser thread
Restful API design specification
MySQL learning record 10getting started with JDBC
[NVIDIA development board] FAQ (updated from time to time)
JVM performance tuning and practical basic theory - Part 1