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".

思路:这题的思路和basic calculator一样,我们构建一个helper fuction,用来处理没有[]的情况,basic calculator是处理有()的情况。也就是说,如果遇到有[],我们需要把[]的起始点和终点找到,
然后call那个helper function, 直到传入到helper function的string不含有[].

 class Solution {
public String decodeString(String s) {
return helper(s, , s.length() - );
} private String helper(String s, int start, int end) {
StringBuilder sb = new StringBuilder();
for (int i = start; i <= end; i++) {
if (isDigit(s.charAt(i))) {
int num = ; //取出数字
while(i <= end && s.charAt(i) != '[') {
num = num * + s.charAt(i) - '';
i++;
}
int count = , j = i + ; //取出[]的起始点,起点是j - 1, 终点是 i。
while(i < end) {
if (s.charAt(i) == '[') {
count++;
} else if (s.charAt(i) == ']') {
count--;
}
if (count == ) {
break;
}
i++;
}
String str = helper(s, j, i - );
sb.append(generateString(str, num));
} else {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
private String generateString(String s, int count) {
StringBuilder str = new StringBuilder();
for (int i = ; i <= count; i++) {
str.append(s);
}
return str.toString();
} private boolean isDigit(char ch) {
return ch >= '' && ch <= '';
}
}

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] 394. Decode String 解码字符串

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

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

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

  4. [Swift]LeetCode394. 字符串解码 | Decode String

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

  5. 394. Decode String

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

  6. [LeetCode] Decode String 题解

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

  7. Leetcode -- 394. Decode String

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

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

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

  9. 解码字符串 Decode String

    2018-11-14 17:56:12 问题描述: 问题求解: 方法一.递归求解 最直观的解法就是递归来求了,并且很显然的这个问题可以使用递归来进行求解. public String decodeSt ...

随机推荐

  1. vue keepalive 动态设置缓存

    场景:A首页.B列表页.C详情页B---->C 缓存‘列表1’详情的数据A---->C 读取‘列表1’详情的数据B---->C (希望清除‘列表1’的缓存,变成缓存‘列表2’详情的数 ...

  2. MediaManager配置公网访问功能

    安装时设置传输本地地址及端口,如图: 路由器设置端口映射,如下图 使用时,打开公网地址http://IpAddress:8090/ContentManager/MainPage.aspx?zh-CN# ...

  3. Ubuntu18.04LTS安装Nvidia显卡

    笔者在为Ubuntu18.04LTS安装Nvidia显卡驱动之前,早就听说了一系列关于由于Nvidia驱动引起的疑难杂症.选择高质量的教程并保持足够的耐心,就能解 决这些问题.很重要的一点,不要怕把电 ...

  4. 基于 docker 的redis 主从+哨兵(快速部署)

    很简单(字多的步骤见:http://www.cnblogs.com/vipzhou/p/8580495.html) 1.直接启动3个容器 docker network create --subnet ...

  5. js 实现数据结构 -- 集合

    原文: 在Javascript 中学习数据结构与算法. 概念: 即数学中的集合,在计算机科学中被应用成数据结构. 当然,集合中的数据具有不重复的特性.js 集合的原理大致上是 Object 的键值对 ...

  6. vs code 的便捷使用

    鼠标滚动 改变字体大小 打开编辑器设置,搜索 editor.mouseWheelZoom  或者文本设置 自动保存 打开设置 搜索  autosave

  7. Go语言中的Package问题

    问题一.Go使用Package组织源码的好处是什么? 1.任何源码属于一个包 2.用包组织便于代码的易读和复用 问题二.Go语言中Package的种类 Go语言中存在两种包.一种是可执行程序的包.一种 ...

  8. Codeforces 1095F Make It Connected(最小生成树)

    题目链接:Make It Connected 题意:给定一张$n$个顶点(每个顶点有权值$a_i$)的无向图,和已连接的拥有边权$w_i$的$m$条边,顶点u和顶点v直接如果新建边,边权为$a_u+a ...

  9. Alice and Bob HDU - 4111 (SG函数)

    Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The ...

  10. PHP7 中 ?? 与? :的区别

    ??是PHP7版本的新特性,它与?:的区别在哪里呢 ?? $b = $a?? $c ;相当于$b= isset($a)?$a:$c; ?: $b = $a?$a: $c 则是 $b = !empty( ...