当前位置:网站首页>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;
};
边栏推荐
- Purpose of computer F1-F12
- Promise 在uniapp的简单使用
- 自动化测试框架有什么作用?上海专业第三方软件测试公司安利
- Current situation and trend of character animation
- Detailed explanation of heap sorting
- pytorch训练好的模型在加载和保存过程中的问题
- Alibaba cloud server mining virus solution (practiced)
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 使用latex导出IEEE文献格式
猜你喜欢

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

Using C language to complete a simple calculator (function pointer array and callback function)

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

C语言深度解剖——C语言关键字

Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

Computer cleaning, deleted system files
![[MySQL] limit implements paging](/img/94/2e84a3878e10636460aa0fe0adef67.jpg)
[MySQL] limit implements paging

Mongodb installation and basic operation

After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
随机推荐
Export IEEE document format using latex
Esp8266-rtos IOT development
C语言深度解剖——C语言关键字
torch建立的网络模型使用torchviz显示
egg. JS project deployment online server
角色动画(Character Animation)的现状与趋势
[MySQL] limit implements paging
UML diagram memory skills
Unsupported operation exception
PC easy to use essential software (used)
Pytorch view tensor memory size
LeetCode:剑指 Offer 42. 连续子数组的最大和
opencv+dlib实现给蒙娜丽莎“配”眼镜
Deep analysis of C language data storage in memory
TCP/IP协议
C语言双指针——经典题型
电脑F1-F12用途
UnsupportedOperationException异常
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
ROS compilation calls the third-party dynamic library (xxx.so)