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 ...
随机推荐
- 自制 yum 源仓库
转自:https://www.cnblogs.com/marility/p/7278374.html 背景 客户服务器为内网机器,centos7系统,且无法与外网连接.需要部署对应的LANMP环境及其 ...
- 元祖(tuple)
元祖和列表几乎是一样的: 列表是可以进行修改的,它可以填加一个元素,也可以移除一个元素,但元祖是不能修改的 如果我们以后想导一个集合,这个集合以后不允许修改,我们用元祖:如何我们想让别人进行修改,我们 ...
- libpng warning:iCCP:known incorrect sRGB profile
原因是新版的libpng增强了检查,发出警告.此警告可以忽略.若要消除此警告则要使用v4的色彩配置.GIMP sRGB v4 色彩配置,修改当前图片的色彩配置,设为默认. sRGB profilesO ...
- Hdu 5344
Hdu5344 题意: 给你一个数组,求所有的 $ a_i + a_j $ 的异或值. 解法: 因为 $ (a_i+a_j) \bigoplus (a_j + a_i) = 0$ . 所以答案就是 $ ...
- 【Robot Framework 项目实战 02】使用脚本生成统一格式的RF关键字
背景 在微服务化的调用环境下,测试数据及接口依赖的维护是一个问题,因为依赖的接口和数据可能不在同一个服务下,而这相关的多个服务往往是不同人员来测试的. 因此为了节省沟通成本,避免关键字的重复冗余.所以 ...
- Linux 文件存在程序找不到文件
1. 编码格式 程序运行时的编码格式和传输到程序中参数的编码格式是否一致,可以在程序中打印日志进行验证: 2. 转义符 文件路径中存在转义符 3. 运行程序的用户身份 不同用户运行程序也可能导致编码格 ...
- 【零基础】AI神经元解析(含实例代码)
一.序言 关于“深度学习”大部分文章讲的都云里雾里,直到看到“床长”的系列教程以及<深度学习入门:基于Python的理论与实现>,这里主要是对这两个教程进行个人化的总结,目标是让“0基础” ...
- spring cloud gateway:Unable to find GatewayFilterFactory with name Hystrix
在springcloud gateway中引用Hystrix filter 编译启动时提示 Unable to find GatewayFilterFactory with name Hystrix ...
- QT 自定义消息
#define TEST_EVENT QEvent::User + 100 class CVxActuatorMain : public QMainWindow { protected: ...
- [spring]数据库的连接配置
使用druid数据源 ,并支持事务处理. <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...