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. Nginx学习总结:proxy与rewrite模块(三)

    斜体下划线,表示建议采用默认配置,无需显式的配置 一.ngx_http_upstream_module 此模块中可配置的指令并不是很多.nginx的负载均衡算法包括: 1)round-robin:轮询 ...

  2. vue,一路走来(13)--vue微信分享

    vue微信分享 今天记录一下vue微信分享. 1.先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”.这个不多说,见文档,只有绑定了才能进行下一步的动作 2.需要引入js文件 ...

  3. 一、表单和ajax中的post请求&&后台获取数据方法

    一.表单和ajax中的post请求&&后台获取数据方法 最近要做后台数据接收,因为前台传来的数据太过于混乱,所以总结了一下前台数据post请求方法,顺便写了下相对应的后台接收方法. 前 ...

  4. MySQL--17 配置binlog-server 及中间件

    目录 配置binlog-server MySQL中间件Atlas Atlas管理接口 配置binlog-server 修改mha配置文件 [root@mysql-db03 ~]# vim /etc/m ...

  5. ltp-ddt smp_basic

    SMP_S_FUNC_DUAL_CORE source functions.sh; cmd="stress-ng --matrix 4 -t 10s --perf --matrix-size ...

  6. ubuntu安装deb包(dpkg)

    安装 sudo dpkg -i DEB_PACKAGE 卸载 sudo dpkg -r PACKAGE_NAME 重新配置已安装的包 Reconfigure an existing package 例 ...

  7. 正则表达式中的Quantifiers

    ?: Match an element zero or one time 例如: colou?r: color 或 colour 但不能是 colo2r *: Match an element zer ...

  8. C/C++ C++ 11 兰姆达

    { auto layer = Layer::create();    auto event = cocos2d::EventListenerTouchOneByOne::create();    ev ...

  9. Angular JS - 4 - Angular JS 作用域与控制器对象

    1. 控制器对象使用 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...

  10. Maven开始

    1:加入Maven插件: 这句话的意思是: 从本地仓库找到相应的jar包 <localRepository>F:\RepMaven</localRepository 2:创建一个Ma ...