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. 电梯调度编写(oo-java编程)

    第二单元的问题是写一个关于电梯调度的程序. 需要模拟一个多线程实时电梯系统,从标准输入中输入请求信息,程序进行接收和处理,模拟电梯运行,将必要的运行信息通过输出接口进行输出. 主要锻炼学生的多线程程序 ...

  2. selenium webdriver 如何实现将浏览器滚动条移动到某个位置

    说明: 在做selenium webdriver  在做UI 自动化时,有些页面时使用懒加载的形式显示页面图片,如果在不向下移动滚动条时,获取到的图片会是网站的默认图片和真实的图片不相符. 所以研究了 ...

  3. MongoDB系列:四、spring整合mongodb,带用户验证

    在前面的两篇博客 MongoDB常用操作练习.springboot整合mongoDB的简单demo中,我们基本上熟悉了mongodb,也把它与spring boot进行了整合并且简单使用.在本篇博客中 ...

  4. 收藏了8年的PHP优秀资源,都给你整理好了

    https://segmentfault.com/a/1190000018071558 最后更新于 2019.02.12 以下是我整理好的 PHP 资源,各位看官拿去用,不用再自己找了. 每周在 Gi ...

  5. 使用react全家桶制作博客后台管理系统

    前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基于react全家桶(React.React-r ...

  6. 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)

    在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...

  7. 洛谷P5289 [十二省联考2019]皮配(01背包)

    啊啊啊边界判错了搞死我了QAQ 这题是一个想起来很休闲写起来很恶心的背包 对于\(k=0\)的情况,可以发现选阵营和选派系是独立的,对选城市选阵营和学校选派系分别跑一遍01背包就行了 对于\(k> ...

  8. Matlab 中subsystem mask封装子系统

    Icon&port %%外型图表封装 %%.曲线型标注: plot(cos(:*pi),sin(:*pi)) %%.文字型标注: disp('PID\n控制器') %%.曲线加文字型标注: p ...

  9. nexus5 root

    LG nexus5 安装新的lineage 14.1系统卡刷 supersuV2.82失败,开机卡动画界面. 改回刷入2016年11月下的cm 13 的包,三清后卡刷supersuV2.82,成功.

  10. hadoop记录-hadoop常用

    1.hdfs目录配额 #设置配额目录hdfs dfsadmin -setSpaceQuota 10T /user/hive/warehouser/tmp查看配额目录信息hdfs dfs -count ...