394. Decode String

Given an encoded string, return it's 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".
算法分析

看到 3[a2[c]] 这样的字符串,显然要用到递归算法,就是要把 [] 中间的字符串解析出来后,再由外面的数字决定重复次数。而解析 [] 中的字符串的过程,就是调用解析函数的过程。对于一个 encoded string, 我们把它考虑成如下的格式: (head)(body)(tail)

其中,head 是纯字符序列(当然也可能为空字符序列),body 是形如 3[ab2[c]] 的序列,tail 是之后的尾端字符序列;tail 可以看做一个独立的 encoded string。我们首先解析出 head,然后对 body 里方括号内的字符串调用解析函数,将得到的字符串重复 body 里打头的数字次数;然后解析 tail ,最后将三者合并即可。

Java算法:

public class Solution {
public String decodeString(String s) {
if(s==null||s.length()==0)
return s;
char ch;
int index=0;
int repeat=0;
StringBuilder head=new StringBuilder(""),body=new StringBuilder("");
while(index<s.length()&&!Character.isDigit(ch=s.charAt(index))){
head.append(ch);
index++;
}
if(index<s.length()){
//indicating that next character is Digit
while(index<s.length()&&Character.isDigit(ch=s.charAt(index))){
repeat=repeat*10+ch-'0';
index++;
}//got the leading num
//now index is pointing to '['; //next, to get the index of ']';
int rightBracket=index+1;
int leftBracketNum=1;
while(leftBracketNum>0){
ch=s.charAt(rightBracket);
if(ch==']'){
leftBracketNum--;
}
else if(ch=='['){
leftBracketNum++;
}
else{ }
rightBracket++;
}
rightBracket--;//now rightBracket is pointing to the right position of the ']';
String bodyStr=decodeString(s.substring(index+1,rightBracket));
String tail=decodeString(s.substring(rightBracket+1)); for(int i=1;i<=repeat;i++){
body.append(bodyStr);
}
body.append(tail);
} return head.toString()+body.toString();
}
}

LeetCode赛题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. 【LeetCode】394. Decode String 解题报告(Python)

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

  3. Leetcode -- 394. Decode String

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

  4. Python 解LeetCode:394 Decode String

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

  5. 【leetcode】394. Decode String

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

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

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

  7. 394 Decode String 字符串解码

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

  8. 394. Decode String

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

  9. LeetCode赛题395----Longest Substring with At Least K Repeating Characters

    395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...

随机推荐

  1. jersey annotations

    参照: http://blog.csdn.net/a19881029/article/details/43056429 官网文档翻译版 @Path 用来为资源类或方法定义URI,当然除了静态URI也支 ...

  2. scp 一次拷贝多个文件

    用正则表达式去匹配即可, scp  *.tar  root@11.11.11.12:/root/ 拷贝当前目录下的所有tar类型的文件到服务器上

  3. 【性能测试】:关于Sockets协议的脚本的开发

    一,关于Sockets协议的脚本,首先对报文的解析是一个关键, 报文的解析一般对着接口文档,弄清楚每个字段代表什么意思,如下一段报文,放在data.ws中 "\x00\x00\x01\x5C ...

  4. PHP-GD库开发手记

    创建带有alpha通道的背景输出图像画中文字体获取长宽等比例缩放图片,也可以用于裁切实例代码 创建带有alpha通道的背景 $png = imagecreatetruecolor(800, 600); ...

  5. [Xarmrin.IOS]使用Build Host 在Windows上建置IOS程式及DeBug (转帖)

    使用Xamarin開發IOS程式時, 必須要在Mac上才可以編譯程式,若想在windows系統上編譯,則可透過Build host的方式, 但還是要有一台Mac的電腦就是了XD 首先你的Mac必須要已 ...

  6. 配置Linux本地源镜像

    今天看到同事做了一个公司本地的apache镜像源,感觉好叼的样子.然后就自己上网找些资料,尝试自己搭建一套出来.然后就有了这篇博文... 声明:本文中充满了浓浓的技术嫉妒的心理,阅读需谨慎. 本文以 ...

  7. PHP之string之str_shuffle()函数使用

    str_shuffle (PHP 4 >= 4.3.0, PHP 5, PHP 7) str_shuffle - Randomly shuffles a string str_shuffle - ...

  8. [中英对照]The sysfs Filesystem | sysfs文件系统

    The sysfs Filesystem | sysfs文件系统 Abstract | 摘要 sysfs is a feature of the Linux 2.6 kernel that allow ...

  9. PTA (Advanced Level) 1012 The Best Rank

    The Best Rank To evaluate the performance of our first year CS majored students, we consider their g ...

  10. [PY3]——内置数据结构(6)——集合及其常用操作

    集合及其常用操作Xmind图          集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...