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".
Solution:
public class Solution {
public String decodeString(String s) {
StringBuilder builder = new StringBuilder();
decodeStringRecur(s.toCharArray(),builder,0);
return builder.toString();
} public int decodeStringRecur(char[] sArr, StringBuilder builder, int start){
if (start>=sArr.length){
return start;
} int p1 = start;
while (p1<sArr.length && sArr[p1]!=']'){
if (sArr[p1]<'0' || sArr[p1]>'9'){
builder.append(sArr[p1++]);
} else {
// get the following encoded string.
// get the number first.
int val = 0;
while (p1<sArr.length && sArr[p1]!='['){
val = val*10 + (int)(sArr[p1++]-'0');
} // get the string.
StringBuilder subBuilder = new StringBuilder();
p1 = decodeStringRecur(sArr,subBuilder,p1+1); // add into decoded string.
for (int i=0;i<val;i++){
builder.append(subBuilder);
}
}
} return (p1<sArr.length) ? p1+1 : p1;
}
}
 
 

LeetCode-Decode String的更多相关文章

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

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

  2. LeetCode——Decode String

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

  3. [LeetCode] Decode String 题解

    题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...

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

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

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

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

  6. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  7. [LeetCode] Decode Ways 解题思路

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  9. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  10. Leetcode -- 394. Decode String

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

随机推荐

  1. JSON 格式介绍

    转自:http://www.json.org/json-zh.html JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器 ...

  2. 读书笔记——Windows核心编程(13)Windows内存体系结构

    对于32位进程(0x0000 0000~0xFFFF FFFF),有4GB的地址空间. 每个进程都有自己专有的地址空间,当进程的各个线程运行时,它们只能访问属于该进程的内存. 这4GB其实是虚拟地址空 ...

  3. 不用synchronized块的话如何实现一个原子的i++?

    上周被问到这个问题,没想出来,后来提示说concurrent包里的原子类.回来学习一下. 一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic ...

  4. JS高级程序设计2nd部分知识要点5

    JS Regexp 字面量模式 用\反斜杠转义 构造函数中的字符串 也用\转义正则也用\ RegExp实例属性 global -布尔值  /g ignoreCase -布尔值 /i lastIndex ...

  5. Oracle常用plsql

    String aggr http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php     SELECT 'S ...

  6. tika提取pdf信息异常

    org.apache.tika.sax.WriteOutContentHandler$WriteLimitReachedException: Your document contained more ...

  7. solr性能调优

    Schema Design Considerations indexed fields indexed fields 的数量将会影响以下的一些性能: 索引时的时候的内存使用量 索引段的合并时间 优化时 ...

  8. hdu2527哈夫曼编码

    /* Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  9. NOIP2007 T2纪念品分组 解题报告-S.B.S.

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  10. 【ASP.NET 基础】ASP.NET内置对象

    准确地说,asp.net 并没有内置对象这一说,jsp 里确实把 request.response 这些当作 jsp 的内置对象,这里只不过是借用了一下 jsp 的说法而已.在 Web 中处于中心的是 ...