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. BigDecimal 算数

    BigDecimal big=new BigDecimal("22.233"); BigDecimal big1=new BigDecimal("12.233" ...

  2. [BZOJ4826] [HNOI2017] 影魔 单调栈 主席树

    题面 因为是一个排列,所以不会有重复的.如果有重复就没法做了.一开始没有仔细看题目想了半天. 发现,如果是第一种情况,那么边界\(l\)和\(r\)就应该分别是整个区间的最大值和次大值. 然后,对于那 ...

  3. HTML知识梳理(笔记)

    HTML常见元素 meta 定义和用法<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标 ...

  4. python2和python3的编码encode解码decode函数

    python比较坑的一个点:意义完全变了的两个函数 首先 常用的编码方式有3种,utf-8:  常用的传输和存储格式,Unicode的一种简化 Unicode:包括了所有可能字符的国际统一编码 GBK ...

  5. react-jsx和数组

    JSX: 1.全称:JavaScriptXML, 2.react定义的一种类似于XML的JS扩展语法:XML+JS 3.作用:用来创建react虚拟DOM(元素)对象 var ele=<h1&g ...

  6. docker 运行jenkins及vue项目与springboot项目(三.jenkins的使用及自动打包vue项目)

    docker 运行jenkins及vue项目与springboot项目: 一.安装docker 二.docker运行jenkins为自动打包运行做准备 三.jenkins的使用及自动打包vue项目 四 ...

  7. mysql 查询所有子节点

    SELECT t3.college_code FROM ( SELECT t1.college_code, IF ( find_in_set( t1.parent_org_code, , ) AS i ...

  8. 迅捷路由器FW325R的无线桥接

    1.恢复路由器默认设置 长按路由器后面的按钮直到指示灯全亮后只剩一个灯亮时松开按钮,此时就已经重置路由器了.然后设置路由器后台密码什么的,那些向导什么的可以跳过 2.高级设置内容 进入路由器高级设置: ...

  9. basic play

    [root@wen ~]# w 19:01:27 up 1 day, 7:06, 3 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN ...

  10. django搭建一个小型的服务器运维网站-重启服务器的进程

    目录 项目介绍和源码: 拿来即用的bootstrap模板: 服务器SSH服务配置与python中paramiko的使用: 用户登陆与session; 最简单的实践之修改服务器时间: 查看和修改服务器配 ...