56.Decode String(解码字符串)
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(解码字符串)的更多相关文章
- [LeetCode] Decode String 解码字符串
		
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
 - [LeetCode] 394. Decode String 解码字符串
		
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
 - 394. Decode String 解码icc字符串3[i2[c]]
		
[抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...
 - LeetCode 394. 字符串解码(Decode String) 44
		
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
 - [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 ...
 - [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 ...
 - Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
		
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
 - [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 ...
 - [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 ...
 
随机推荐
- go中基本数据类型的默认值
			
代码 // 基本数据类型(整型,浮点型,字符串型,布尔型)的默认值 package main import ( "fmt" ) func main() { var a int va ...
 - [烧脑时刻]EL表达式1分钟完事
			
一天,程序员A问我,我们比比谁的知识点多,反应快.我回答:那就看谁最快用EL表达式的显示在页面上吧. 话不多说,计时开始. 项目的结构如上,大概就是一个Family的JavaBean,一个jsp页面, ...
 - 微信小程序(6)--获取屏幕宽度及弹窗滚动与页面滚动冲突
			
1.获取屏幕宽度,并赋值给view <view class="ships-img" style="height:{{windowWidth}}px;"&g ...
 - 【LeetCode】贪心 greedy(共38题)
			
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
 - 解决IDEA项目名称无下标蓝色小方块
			
点击下图中 + 号,引入该工程的pom.xml即可 .
 - Ubuntu简单安装kafka及使用
			
参考地址:https://www.jianshu.com/p/d0e630c8f4ae 一.下载 kafka 二进制安装包 下载地址: http://kafka.apache.org/download ...
 - 牛客网NOIP赛前集训营-提高组(第六场)B-选择题
			
题目描述 有一道选择题,有 a,b,c,d 四个选项. 现在有 n 个人来做这题,第 i 个人有 pi,j 的概率选第 j 个选项. 定义\(cnt(x)\)为选第$ x $个选项的人数. 令\(mx ...
 - ubuntu root用户 phpstorm软件不能使用中文输入法
			
一. 在 ~/.bashrc 里面加入 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export XMODIFIERS="@im ...
 - 您的加密USB驱动器是否安全?黑客又是如何攻破的?
			
您如何确定您使用的“安全”USB驱动器是否真的安全,并且您存储的数据无法提取?这正是Google公司和中国网络安全研究人员在最近的2018年黑帽美国大会上以艰难的方式攻击加密的USB密钥”的问题. 研 ...
 - Java JDK1.8新特性之四大函数式接口
			
JDK 1.8的一些新特性 四大核心函数式接口(Consumer.Predicate.Supplier.Function),结合lambda表达式 import java.util.ArrayList ...