当前位置:网站首页>Leetcode394 string decoding
Leetcode394 string decoding
2022-07-28 12:46:00 【.DoubleBean.】
Title address LeetCode394. String decoding
https://leetcode-cn.com/problems/decode-string/
Title Description
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"
Ideas
Use the stack to save numbers and strings
Code
- Language support :c++
c++ Code:
class Solution {
public:
string decodeString(string s) {
int len = s.size(), num = 0;
stack<int> nums;
stack<string> str;
string res = "";
for(int i=0; i<len; i++){
if(s[i]>='0' && s[i]<='9'){
num *= 10;
num += (s[i]-'0');
}
else if(s[i]>='a' && s[i]<='z'){
res += s[i];
}
else if(s[i]=='['){
nums.push(num);
str.push(res);
res = "";
num = 0;
}
else{
int count = nums.top();
nums.pop();
while(count){
str.top() += res;
count--;
}
res = str.top();
str.pop();
}
}
return res;
}
};
边栏推荐
- The usage and Simulation Implementation of vector in STL
- [cute new problem solving] climb stairs
- 用C语言开发NES游戏(CC65)03、VRAM缓冲区
- 恋爱男女十禁
- Developing NES games (cc65) 05 and palette with C language
- Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
- 洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
- 一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
- AVL tree (balanced search tree)
- 30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held
猜你喜欢

Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself

奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东

MMA8452Q几种模式的初始化实例

sqli-labs(less-8)

苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))

Open source office (ospo) unveils Secrets

LeetCode94. 二叉树的中序遍历

Marketing play is changeable, and understanding the rules is the key!

开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开

新东方单季营收5.24亿美元同比降56.8% 学习中心减少925间
随机推荐
云原生—运行时环境
机器学习实战-神经网络-21
Four authentic postures after suffering and trauma, Zizek
Using dependent packages to directly implement paging and SQL statements
Redis实现分布式锁
Developing NES games with C language (cc65) 04. Complete background
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
C# 泛型是什么、泛型缓存、泛型约束
Leetcode:704 binary search
Aopmai biological has passed the registration: the half year revenue is 147million, and Guoshou Chengda and Dachen are shareholders
1331. 数组序号转换 : 简单模拟题
How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
Developing NES game (cc65) 03 and VRAM buffer with C language
软件架构师必需要了解的 saas 架构设计?
DART 三维辐射传输模型申请及下载
AVL树(平衡搜索树)
Leetcode: array
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
XIII Actual combat - the role of common dependence