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. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

  2. Linux centos ansible

    创建m01.backup.nfs.web01.web02 m01(172.16.1.61).backup(172.16.1.41).nfs(172.16.1.31).web01(172.16.1.7) ...

  3. B树和B+树详解

    一 B树 1.B树的定义:B树(B-tree)是一种树状数据结构,它能够存储数据.对其进行排序并允许以O(log n)的时间复杂度运行进行查找.顺序读取.插入和删除的数据结构.B树,概括来说是一个节点 ...

  4. array数组(n,1)和(n,)互相转换

    data.shape #(172,1) result = [arr[0] for arr in data] result.shape #(172,) (172,1)表示是一个(172,1)shape大 ...

  5. Python Face Detect Offline

    python版本 3.7.0  1. 安装 cmake pip install cmake  2.安装 boost pip install boost  3.安装 dlib pip install d ...

  6. AB PLC教程

      把文档贴到Blog.BBS或个人站等: 复制 预览 普通尺寸(450*500pix) 较大尺寸(630*500pix)

  7. Linux 下安装idea,提示svn版本太低问题

    在 RedHat 6.5 虚拟机上装了 Idea 2017, 将项目代码从 Windows 共享到虚拟机中,然后 Idea 提示 svn 版本太旧, 上网查资料说 Idea 2018 不支持1.7以下 ...

  8. 高新兴 ME3630-W 4G 模块 Android 平台适配

    2019-04-26 关键字:高新兴 ME3630-W 适配.rk3128 移植 4G 模块 本篇文章系笔者在移植 高新兴物联 ME3630-W 4G 模块到运行着 Android4.4 操作系统的 ...

  9. nginx 项目部署

    一.nginx 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web 网络服务是一种被 ...

  10. Flask 构建微电影视频网站(八)

    评论收藏及弹幕 实现电影评论添加及列表.数据查询实现统计播放量和评论量.jquery ajax实现收藏电影,flask结合redis消息队列实现电影弹幕,bug处理等功能. 电影评论-统计 class ...