当前位置:网站首页>LeetCode394 字符串解码
LeetCode394 字符串解码
2022-07-28 11:49:00 【.DoubleBean.】
题目地址 LeetCode394. 字符串解码
https://leetcode-cn.com/problems/decode-string/
题目描述
给定一个经过编码的字符串,返回它解码后的字符串。
编码规则为: 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"
思路
用栈来保存数字以及字符串
代码
- 语言支持: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;
}
};
边栏推荐
- 一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
- How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
- JSP自定义标签之自定义分页标签02
- Siemens docking Leuze BPS_ 304i notes
- Communication example between upper computer and Mitsubishi fn2x
- The usage and Simulation Implementation of vector in STL
- [cute new problem solving] climb stairs
- 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
- 苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
- 用C语言开发NES游戏(CC65)02、什么是v-blank?
猜你喜欢

With the continuous waves of infringement, the U.S. patent and trademark office began to study the impact of NFT on copyright

sqli-labs(less-8)

Library automatic reservation script

揭秘界面控件DevExpress WinForms为何弃用受关注的MaskBox属性

试用copilot过程中问题解决

Open source office (ospo) unveils Secrets

软件架构师必需要了解的 saas 架构设计?

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?

界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论

VS code更新后不在原来位置
随机推荐
试用copilot过程中问题解决
Using dependent packages to directly implement paging and SQL statements
Implementation method of mouse hover, click and double click in ue4/5
Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production
开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
Holes in [apue] files
C structure use
Initialization examples of several modes of mma8452q
SuperMap game engine license module division
Let Arduino support nuvotom Xintang
Developing NES games with C language (cc65) 11. Metatiles
Analysis of new retail e-commerce o2o model
Developing NES games with C language (cc65) 04. Complete background
Why do enterprises need the ability of enterprise knowledge management?
Developing NES games with C language (cc65) 02. What is v-blank?
1331. Array sequence number conversion: simple simulation question
牛客网二叉树题解
连通块&&食物链——(并查集小结)
洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
Redis implements distributed locks