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. Web前端之iframe详解

    iframe基本内涵 通常我们使用iframe直接直接在页面嵌套iframe标签指定src就可以了. <iframe src="demo_iframe_sandbox.htm" ...

  2. 通知实战 设置通知图片(iOS10以后的)

    解释两个基本扩展(Notification Content.Notification Service) Notification Content其实是用来自定义长按通知显示通知的自定义界面 Notif ...

  3. C语言报错:error: expected ‘while’ at end of input } ^

    在建线程池过程当中遇见上图所示错误: 解决方法: Linux中定义: SYNOPSIS #include <pthread.h> void pthread_cleanup_push(voi ...

  4. Robust PCA via Outlier Pursuit

    目录 引 主要结果 定理1 定理2 理论证明 构造Oracle Problem 算法 Xu H, Caramanis C, Sanghavi S, et al. Robust PCA via Outl ...

  5. 软工+C(9): 助教指南,持续更新...

    上一篇:提问与回复 下一篇:从命令行开始逐步培养编程能力(Java) 目录: ** 0x00 Handshake ** 0x01 点评 ** 0x02 评分 ** 0x03 知识储备 ** 0x04 ...

  6. Hexo之傻瓜攻略

    Hexo搭建个人技术博客的网站,从零开始流程 搭建篇 必备Node.js环境 安装配置hexo: npm install -g hexo 安装cli hexo init 初始化hexo npm ins ...

  7. Python------Mongodb操作

    Python3要操作Mongodb需要下载pymongo,Linux下获取pymongo的方法也比较简单,控制台输入命令:sudo pip3 install pymongo 即可. Pymongo的文 ...

  8. 微信小程序爬坑

    1.app.json配置信息是怎样的? { "pages":[ "pages/页面1/页面1", "pages/页面2/页面2", ], & ...

  9. Ubuntu系统桌面任务栏和启动器全部消失解决方案

    ubuntu桌面上没有启动器,没有任务栏,只有一个背景,但是运行正常.这种情况很可能是文件管理程序出现异常了. 解决办法: Ctrl+Alt+F1 进入命令行,输入: sudo service lig ...

  10. ShoppingCart

    数据库设计 表结构 [dbo].[AdminInfo] AdminID, AdminName, AdminPassword, RoleID [dbo].[BK_Car] ID, CarID, ISBN ...