当前位置:网站首页>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;
};
边栏推荐
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- C语言深度解剖——C语言关键字
- JVM performance tuning and practical basic theory - Part 1
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
- Restful API design specification
- TP-LINK 企业路由器 PPTP 配置
- 2022.02.13 - NC002. sort
- 生成器参数传入参数
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- ROS compilation calls the third-party dynamic library (xxx.so)
猜你喜欢

Detailed explanation of heap sorting

After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF

Beijing invitation media

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

【嵌入式】Cortex M4F DSP库

Swagger setting field required is mandatory

优秀的软件测试人员,都具备这些能力

Deep analysis of C language data storage in memory

Roguelike游戏成破解重灾区,如何破局?

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
随机推荐
Beijing invitation media
电脑清理,删除的系统文件
电脑F1-F12用途
What are the common processes of software stress testing? Professional software test reports issued by companies to share
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
可变长参数
visdom可视化实现与检查介绍
Purpose of computer F1-F12
2022.02.13 - NC003. Design LRU cache structure
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
2022.02.13 - NC001. Reverse linked list
Problems in loading and saving pytorch trained models
Trying to use is on a network resource that is unavailable
Sort according to a number in a string in a column of CSV file
C language double pointer -- classic question type
China polyether amine Market Forecast and investment strategy report (2022 Edition)
如何进行接口测试测?有哪些注意事项?保姆级解读
poi追加写EXCEL文件