当前位置:网站首页>LeetCode:394. String decoding
LeetCode:394. String decoding
2022-07-06 08:50:00 【Bertil】
Given an encoded string , Returns the decoded string .
The coding rule is : k[encoded_string], Represents the encoded_string Just repeat k Time . Be careful k Positive integer guaranteed .
You can think that the input string is always valid ; There are no extra spaces in the input string , And the square brackets entered always meet the format requirements .
Besides , You can assume that the raw data does not contain numbers , All numbers only indicate the number of repetitions k , For example, there is no such thing as 3a or 2[4] The input of .
Example 1:
Input :s = "3[a]2[bc]"
Output :"aaabcbc"
Example 2:
Input :s = "3[a2[c]]"
Output :"accaccacc"
Example 3:
Input :s = "2[abc]3[cd]ef"
Output :"abcabccdcdcdef"
Example 4:
Input :s = "abc3[cd]xyz"
Output :"abccdcdcdxyz"
Tips :
- 1 <= s.length <= 30
- s By lowercase letters 、 Numbers and square brackets ‘[]’ form
- s Guarantee is a It works The input of .
- s The value range of all integers in is [1, 300]
Their thinking
1. From inside to outside , Solve layer by layer [ ], Need to keep memory of characters , So use the stack
2. Time to enter the stack : encounter [ It means solving internal problems first , External numbers and letters go to the stack and wait
- When you meet [, The number that has been scanned is “ Multiple ”, Stack temporarily
- When you meet [, The scanned letters are also stacked and waiting , Wait until the decoding in brackets is finished , Can participate in the construction of strings together
3. Time to get out of the stack : encounter ] It means that the scanning of the inner layer is finished , Top element of stack ( I met you recently “ Multiple ” And letters ) You can get out of the stack , Jointly participate in the construction of substrings
Code
/** * @param {string} s * @return {string} */
var decodeString = function(s) {
let numStack = []; // Stack of multiples
let strStack = []; // save To be spliced str The stack
let num = 0; // Temporary multiple
let result = ''; // Temporary string
for (const char of s) {
// Character by character scanning
if (!isNaN(char)) {
// When it comes to numbers
num = num * 10 + Number(char); // Calculate multiple
} else if (char == '[') {
// encounter [
strStack.push(result); // result String into stack
result = '';
numStack.push(num); // Multiple num Enter the stack and wait
num = 0;
} else if (char == ']') {
// encounter ], The stack of two stacks comes out of the stack
let repeatTimes = numStack.pop(); // Number of copies obtained
result = strStack.pop() + result.repeat(repeatTimes); // Build substrings
} else {
result += char; // Letters encountered , Append to result strand
}
}
return result;
};
边栏推荐
- 如何有效地进行自动化测试?
- win10系统中的截图,win+prtSc保存位置
- 【嵌入式】使用JLINK RTT打印log
- Tdengine biweekly selection of community issues | phase III
- LeetCode:836. 矩形重叠
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- LeetCode:39. 组合总和
- [NVIDIA development board] FAQ (updated from time to time)
- Unsupported operation exception
猜你喜欢
随机推荐
Swagger setting field required is mandatory
使用latex导出IEEE文献格式
Problems encountered in connecting the database of the project and their solutions
egg. JS getting started navigation: installation, use and learning
ROS compilation calls the third-party dynamic library (xxx.so)
LeetCode:剑指 Offer 04. 二维数组中的查找
优秀的软件测试人员,都具备这些能力
JVM quick start
Problems in loading and saving pytorch trained models
电脑清理,删除的系统文件
Roguelike game into crack the hardest hit areas, how to break the bureau?
查看局域网中电脑设备
Deep analysis of C language pointer
LeetCode:26. 删除有序数组中的重复项
LeetCode:236. 二叉树的最近公共祖先
sublime text中conda环境中plt.show无法弹出显示图片的问题
704 binary search
The harm of game unpacking and the importance of resource encryption
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Promise 在uniapp的简单使用