LeetCode-Decode String
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".
public class Solution {
public String decodeString(String s) {
StringBuilder builder = new StringBuilder();
decodeStringRecur(s.toCharArray(),builder,0);
return builder.toString();
}
public int decodeStringRecur(char[] sArr, StringBuilder builder, int start){
if (start>=sArr.length){
return start;
}
int p1 = start;
while (p1<sArr.length && sArr[p1]!=']'){
if (sArr[p1]<'0' || sArr[p1]>'9'){
builder.append(sArr[p1++]);
} else {
// get the following encoded string.
// get the number first.
int val = 0;
while (p1<sArr.length && sArr[p1]!='['){
val = val*10 + (int)(sArr[p1++]-'0');
}
// get the string.
StringBuilder subBuilder = new StringBuilder();
p1 = decodeStringRecur(sArr,subBuilder,p1+1);
// add into decoded string.
for (int i=0;i<val;i++){
builder.append(subBuilder);
}
}
}
return (p1<sArr.length) ? p1+1 : p1;
}
}
LeetCode-Decode String的更多相关文章
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- LeetCode——Decode String
Question Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string ...
- [LeetCode] Decode String 题解
题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
随机推荐
- 通知中心NSNotificationCenter的使用
通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...
- 一步步学敏捷开发:6、Scrum的3种工件
Scrum的3种工件包括:Product Blacklog.Sprint Backlog.完成标准. 1.产品待办事项列表(Product Backlog) 产品Blacklog是Scrum中的核心工 ...
- node.js之看懂package.json依赖库版本控制
金天:学习一个新东西,就要持有拥抱的心态,如果固守在自己先前的概念体系,就会有举步维艰的感觉.node.js依赖库的版本控制 一般node.js项目会依赖大量第三方module, 那么如何控制modu ...
- PHP开发第一个扩展
首先声明:我们要构建的是扩展或者模块名为hello_module.该模块提供一个方法:hello_word. 一.PHP环境的搭建 1)一般使用源码包编译安装,而不是binary包安装.因为使用PHP ...
- PHP面试题集之基础题
1.用PHP打印出前一天的时间格式是 2006-5-10 22:21:21 date_default_timezone_set('PRC'); //默认时区 echo "今天:", ...
- netty-socketio
一.简介 netty-socketio是一个开源的Socket.io服务器端的一个java的实现,它基于Netty框架.项目地址为:https://github.com/mrniko/netty-so ...
- 【原创】Java实现手机号码归属地查询
网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷. 总结一下这些缺陷: 1.要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属 ...
- matlab2015b调用摄像头
参考链接:http://blog.csdn.net/lyqmath/article/details/7307429 本人电脑是宏碁T5000 调用代码: % By lyqmathclc; clear ...
- A daemon process class in python
In everbright task schedule project, we need some daemon process to do certain work, here is a examp ...
- 【实践】获取CKEditor的html文本、纯文本、被选中的内容及赋值
<%=Html.TextAreaFor(Model => Model.WORK_INTRODUCTION)%> <script type="text/javasc ...