问题描述

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的更多相关文章

  1. [LeetCode] 394. Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  2. 394. Decode String 解码icc字符串3[i2[c]]

    [抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...

  3. 394. Decode String

    [题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...

  4. Leetcode -- 394. Decode String

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  5. 394 Decode String 字符串解码

    给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...

  6. 【LeetCode】394. Decode String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  7. Python 解LeetCode:394 Decode String

    题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...

  8. 【leetcode】394. Decode String

    题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...

  9. LeetCode 394. 字符串解码(Decode String) 44

    394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...

随机推荐

  1. ES6 import、export的写法

    大家都知道来到ES6版本,ES就原生支持JS Module的概念. import和export的写有哪些呢,我们看看 import: import from 和 var 变量一样,也会存在提升,这意味 ...

  2. HDU 1114 Piggy-Bank ——(完全背包)

    差不多是一个裸的完全背包,只是要求满容量的最小值而已.那么dp值全部初始化为inf,并且初始化一下dp[0]即可.代码如下: #include <stdio.h> #include < ...

  3. Server2012R2实现活动目录(Active Directory)双域控制器互为冗余

    在活动目录中部署两台主控域控制器,两台域控制器互为冗余. Server 2012 R2新建活动目录和DC refer to: https://www.cnblogs.com/jfzhu/p/40061 ...

  4. 转载---WCF、WPF、Silverlight和区别

    转自--http://hi.baidu.com/wl5026442/item/6ce62b4d19ff64e61381da9c SilverLight可以看作是WPF的一个简化版本,或者一个轻量版本. ...

  5. LINUX 上 实现SNTP (时间同步)

    Network Time Protocol (NTP) 也是RHCE新增的考试要求. 学习的时候也顺便复习了一下如何设置Linux的时间,现在拿出来和大家分享 设置NTP服务器不难但是NTP本身是一个 ...

  6. 美国top200药品2

     python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  7. SQL-W3School-基础:SQL WHERE 语句

    ylbtech-SQL-W3School-基础:SQL WHERE 语句 1.返回顶部 1. WHERE 子句用于规定选择的标准. WHERE 子句 如需有条件地从表中选取数据,可将 WHERE 子句 ...

  8. js传值到后台乱码问题

    1.前台js的路径 var addurl1="ldcOrderController.do?goods&orderGoodsExtra="+encodeURI(encodeU ...

  9. Cinder LVM Oversubscription in thin provisioning

    目录 文章目录 目录 Oversubscription in thin provisioning Oversubscription in thin provisioning Cinder spec: ...

  10. Kibana Query Language(KQL)

    语法: 官方文档 If you’re familiar with Kibana’s old lucene query syntax, you should feel right at home wit ...