问题描述

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. Linux之创建多个子进程

    /*** fork_test.c ***/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> int ...

  2. E.Substring Reverse Gym - 101755E

    Substring Reverse Problem Two strings s and t of the same length are given. Determine whether it is ...

  3. AE开发之txt转shp

    实现坐标txt文件转shp点集数据文件的窗体Form txt格式为:首行为“id,x,y” 第二行开始输入具体数值:id,x,y(x,y为具体的xy坐标) using System; using Sy ...

  4. JAVA基础知识|小知识点

    1.强烈建议,不使用char类型 那么,到底为什么java里不推荐使用char类型呢?其实,1个java的char字符并不完全等于一个unicode的字符.char采用的UCS-2编码,是一种淘汰的U ...

  5. golang入门time与string转换, time加减时间, 两个时间差

    package main import ( "fmt" "time") var timeLayoutStr = "2006-01-02 15:04:0 ...

  6. 无法访问com.alibaba.fastjson.parser.deserializer.PropertyProcessable

    某项目加入了某依赖A,IDEA里编译报了如下错误: 无法访问com.alibaba.fastjson.parser.deserializer.PropertyProcessable 错误代码行为某个使 ...

  7. kotlin之lambda表达式和匿名函数

    lambda表达式,称为匿名函数,是一种函数字面值,也就是没有声明的函数,但可以作为表达式传递出去. 函数类型: 对于接受另一个函数的作为自己的参数,必须针对这个参数指定一个函数的类型如 fun &l ...

  8. kotlin 泛型约束

    fun <T:Comparable<T>> sort(list :List<T>){} 冒号之后指定的类型就是泛型参数的上界,对于泛型参数T,只允许使用Compar ...

  9. hadoop2.7.7+habse2.0.5+zookeeper3.4.14+hive2.3.5单机安装

    环境 腾讯云centos7 1.hadoop下载 http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.7.7/hadoop-2.7.7.tar ...

  10. logback 和 log4j对比,及相关配置

    Logback 一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和log ...