Level:

  Medium

题目描述:

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

思路分析:

  设置两个栈,一个栈countstack保存方框内字符串的重复次数,一个栈resstack保存目前解码的字符串,用res记录结果,当遇到字符' ] '时,我们弹出countstack的栈顶元素reaptetime,然后将res重复reaptetime次,放入resstack栈中。

代码:

public class Solution{
public String decodeString(String s){
Stack<Integer>countstack=new Stack<>();
Stack<String>resstack=new Stack<>();
String res=""; //保存最终结果
int i=0;
while(i<s.length()){
if(Character.isDigit(s.charAt(i))){ //计算重复次数
int count=0;
while(Character.isDigit(s.charAt(i))){
count=count*10+(s.charAt(i)-'0');
i++;
}
countstack.push(count);
}else if(s.charAt(i)=='['){
resstack.push(res); //将之前解码的字符串存放到栈中
res=""; //重置res
i++;
}else if(s.charAt(i)==']'){
int reaptetime=countstack.pop();
StringBuilder temp=new StringBuilder();
for(int j=0;j<reaptetime;j++){
temp.append(res);
}
res=resstack.pop()+temp.toString();//当前解码结果
i++;
}else{
res=res+s.charAt(i);
i++;
}
}
return res;
}
}

56.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. 394. Decode String 解码icc字符串3[i2[c]]

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

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

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

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [Swift]LeetCode271. 加码解码字符串 $ Encode and Decode Strings

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  8. [Swift]LeetCode880. 索引处的解码字符串 | Decoded String at Index

    An encoded string S is given.  To find and write the decodedstring to a tape, the encoded string is ...

  9. [LeetCode] 271. Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

随机推荐

  1. linux系统使用和优化的原则

  2. shell --08 例子

    目录 1.按照时间生成文件2018-05-22.log将每天的磁盘使用状态写入到对应日期的文件 [root@web01 ceshi]# cat 1.sh #!/bin/bash Date=`date ...

  3. python常用函数 O

    OrderedDict() 保持dict元素插入顺序. 例子: open(path) 可以对文件进行操作,有'r'读模式.'w'写模式.'a'追加模式.'b'二进制模式.'+'读/写模式等,操作完需要 ...

  4. sd卡无法启动及zc706更改主频后可以进入uboot无法启动kernel的坑

    好长的标题 +_+ 1.sd卡无法启动 起因:kernel底下通过dd测试速度,擦写了sd卡,再启动时发现无法启动 于是重新格式化,再将BOOT.bin 相关dtb u-rootfs zImage和u ...

  5. mybatis基本查询

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

  6. .net core webapi添加swagger

    依赖项——右键——管理NuGet程序包——浏览——输入以下内容 Install-Package Swashbuckle.AspNetCore -Pre 双击Properties——点击生成——勾选XM ...

  7. BZOJ3038 上帝造题的七分钟

    Time Limit: 3 Sec Memory Limit: 128 MB Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. "第一分钟,X说, ...

  8. 厉害了!新AI人工智能研究令人大开眼界

    AI人工智能有很多方法可以操纵照片,让您看起来更好看,消除红眼或镜头眩光等等.但到目前为止,眨眼已经证明了一个顽强的快照对手. 这远远不是智能“绘画中”的唯一例子,因为当一个程序用它认为属于的地方填充 ...

  9. Python 分段利润提成

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万之 ...

  10. centos 6.5 解压 tar.gz

    只查看 tar.gz 文件内容而不解压 tar -tvf filename.tar.gz 解压为 tar 文件 gzip -d filename.tar.gz 解压为文件或目录 tar xvf fil ...