当前位置:网站首页>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;
};
边栏推荐
- Current situation and trend of character animation
- JVM performance tuning and practical basic theory - Part 1
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- MySQL learning record 10getting started with JDBC
- ROS compilation calls the third-party dynamic library (xxx.so)
- What is CSRF (Cross Site Request Forgery)?
- Esp8266-rtos IOT development
- View computer devices in LAN
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Roguelike game into crack the hardest hit areas, how to break the bureau?
猜你喜欢
Beijing invitation media
延迟初始化和密封类
Charging interface docking tutorial of enterprise and micro service provider platform
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
Deep analysis of C language data storage in memory
ESP8266-RTOS物联网开发
【嵌入式】使用JLINK RTT打印log
Delay initialization and sealing classes
Unified ordering background interface product description Chinese garbled
Process of obtaining the electronic version of academic qualifications of xuexin.com
随机推荐
visdom可视化实现与检查介绍
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Bitwise logical operator
Tcp/ip protocol
Current situation and trend of character animation
生成器参数传入参数
Restful API design specification
JS pure function
Trying to use is on a network resource that is unavailable
查看局域网中电脑设备
优秀的软件测试人员,都具备这些能力
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
深度剖析C语言指针
可变长参数
Process of obtaining the electronic version of academic qualifications of xuexin.com
Hutool gracefully parses URL links and obtains parameters
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
China polyether amine Market Forecast and investment strategy report (2022 Edition)
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)