LC 394. Decode String
问题描述
Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef"
参考答案
class Solution{
public:
string decodeString(string s) {
int pos = ;
return foo(pos, s);
}
string foo(int& pos, string s){
int num = ;
string word = "";
for(;pos<s.size();++pos){ // 3[ab] = ababab
char cur = s[pos];
if(cur >='' && cur <= ''){
num = num * + cur - ''; // 计算出倍数
}else if(cur == ']'){
return word; // 结束
}else if(cur == '['){
string curStr = foo(++pos, s); // 如果遇到 [ ,将 pos 向后移动一位
for(;num>;num--) word += curStr; // 上一行获得了curStr,而num是上一个循环准备好了,所以可以直接使用。
}else{
word += cur; // 这个是为了应对平常的 char -> return curStr
}
}
return word;
}
};
额外说明
灵魂
这个答案的灵魂,在于当 s[pos] == “[” 的时候,foo( ++pos, s)。
数字,代表×的倍率,一定会出现的。
[ ,代表会出现要处理的字符串,因此这一栏有 string curStr,并且由于有 num 了,所以处理拼合好的字符,也是在这里进行处理的。
],代表着所有递归的结束,不论是大循环,还是小循环,返回 word。
else,也是灵魂,一定意味着normal character,所以直接附在word后面即可。等遇到了 ] ,直接返回,交给 刚才的 ] 里面的 for 处理。
数字,从string到int
int num = 0;
for cur in string:
num = num * 10 + cur - '0';
LC 394. Decode String的更多相关文章
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 394. Decode String 解码icc字符串3[i2[c]]
[抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 394 Decode String 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...
- 【LeetCode】394. Decode String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- Python 解LeetCode:394 Decode String
题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
随机推荐
- vscode设置VUE eslint开发环境
我的使用vscode开发VUE的常用设置 1.安装插件 ESLint Vetur Beautify Prettier - Code formatter Auto Rename Tag -重命名标签,闭 ...
- Spring注解驱动——组件注册系列
1.@Configuration 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被Annot ...
- Feeding Chicken
D - Feeding Chicken 从左上角开始,往右下角开始遍历,但是遍历的时候需要注意一点,就是遍历的时候需要连起来,就比如第一行从左往右进行遍历,但是第二行不能从左往右了,因为这样就分开了, ...
- JAVA基础知识|类设计技巧
1.一定要保证数据私有 2.一定要对数据初始化 3.不要再类中使用过多的基本类型 4.不是所有的域都需要独立的域访问器和域更改器 5.将职责过多的类进行分解 6.类名和方法名要能够体现它们的职责 7. ...
- Linux发行版的选择
1,需要稳定的服务器,选择CentOS 或 RHEL 2,需要自己定制的桌面系统,选择Ubuntu 3,摸索linux 各方面的知识,选择Gentoo 4,需要稳定性高的系统,选择FreeBSD 5, ...
- PYTHON -----pyinstaller的安装
这几天一直在安装pyinstaller库,发现了一个好方法 因为一直使用pip安装,我只能介绍一下pip安装pyinstaller的方法的注意事项 1:一般pip会在安装python的Scripts文 ...
- [SpringBoot/SpringMVC]从Webapp下载一个大文件出现java.lang.OutOfMemoryError: GC overhead limit exceeded怎么办?
本文示例工程下载:https://files.cnblogs.com/files/xiandedanteng/WebFileDownload20191026.rar 制作一个Webapp,让其中一个网 ...
- Maven IntelliJ
IntelliJ IDEA 已经内建了对 Maven 的支持.我们在此例中使用的是 IntelliJ IDEA 社区版 11.1. IntelliJ IDEA 的一些特性列出如下: 可以通过 Inte ...
- features its own
Gulp.js features its own built-in watch() method - no external plugin required ---- However, the Arn ...
- OnPreInit,OnInit ,OnInitComplete ,OnPreLoad ,Page_Load等执行顺序
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...