当前位置:网站首页>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;
};
边栏推荐
- torch建立的网络模型使用torchviz显示
- Cesium draw points, lines, and faces
- 深度剖析C语言数据在内存中的存储
- Deep anatomy of C language -- C language keywords
- Visual implementation and inspection of visdom
- Current situation and trend of character animation
- LeetCode:剑指 Offer 04. 二维数组中的查找
- Simple use of promise in uniapp
- 有效提高软件产品质量,就找第三方软件测评机构
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
猜你喜欢
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Problems in loading and saving pytorch trained models

Cesium draw points, lines, and faces

LeetCode:236. 二叉树的最近公共祖先

深度剖析C语言数据在内存中的存储

Delay initialization and sealing classes

ROS compilation calls the third-party dynamic library (xxx.so)

Alibaba cloud server mining virus solution (practiced)

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

Roguelike game into crack the hardest hit areas, how to break the bureau?
随机推荐
C language double pointer -- classic question type
Function coritization
JVM quick start
Super efficient! The secret of swagger Yapi
How to conduct interface test? What are the precautions? Nanny level interpretation
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
Charging interface docking tutorial of enterprise and micro service provider platform
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
如何进行接口测试测?有哪些注意事项?保姆级解读
【剑指offer】序列化二叉树
What is CSRF (Cross Site Request Forgery)?
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
Compétences en mémoire des graphiques UML
poi追加写EXCEL文件
Variable length parameter
TDengine 社区问题双周精选 | 第三期
sublime text中conda环境中plt.show无法弹出显示图片的问题
marathon-envs项目环境配置(强化学习模仿参考动作)