LeetCode赛题394----Decode String
394. 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".
算法分析
看到 3[a2[c]] 这样的字符串,显然要用到递归算法,就是要把 [] 中间的字符串解析出来后,再由外面的数字决定重复次数。而解析 [] 中的字符串的过程,就是调用解析函数的过程。对于一个 encoded string, 我们把它考虑成如下的格式: (head)(body)(tail)
其中,head 是纯字符序列(当然也可能为空字符序列),body 是形如 3[ab2[c]] 的序列,tail 是之后的尾端字符序列;tail 可以看做一个独立的 encoded string。我们首先解析出 head,然后对 body 里方括号内的字符串调用解析函数,将得到的字符串重复 body 里打头的数字次数;然后解析 tail ,最后将三者合并即可。
Java算法:
public class Solution {
public String decodeString(String s) {
if(s==null||s.length()==0)
return s;
char ch;
int index=0;
int repeat=0;
StringBuilder head=new StringBuilder(""),body=new StringBuilder("");
while(index<s.length()&&!Character.isDigit(ch=s.charAt(index))){
head.append(ch);
index++;
}
if(index<s.length()){
//indicating that next character is Digit
while(index<s.length()&&Character.isDigit(ch=s.charAt(index))){
repeat=repeat*10+ch-'0';
index++;
}//got the leading num
//now index is pointing to '[';
//next, to get the index of ']';
int rightBracket=index+1;
int leftBracketNum=1;
while(leftBracketNum>0){
ch=s.charAt(rightBracket);
if(ch==']'){
leftBracketNum--;
}
else if(ch=='['){
leftBracketNum++;
}
else{
}
rightBracket++;
}
rightBracket--;//now rightBracket is pointing to the right position of the ']';
String bodyStr=decodeString(s.substring(index+1,rightBracket));
String tail=decodeString(s.substring(rightBracket+1));
for(int i=1;i<=repeat;i++){
body.append(bodyStr);
}
body.append(tail);
}
return head.toString()+body.toString();
}
}
LeetCode赛题394----Decode String的更多相关文章
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Python 解LeetCode:394 Decode String
题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- 394. Decode String 解码icc字符串3[i2[c]]
[抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...
- 394 Decode String 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
- LeetCode赛题395----Longest Substring with At Least K Repeating Characters
395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...
随机推荐
- eclipse如何设置UTF-8
一.Eclipse设置utf-8编码包括两个方面,一方面可以设置workspace工作间编码,另一方面可以设置Android Project项目编码,设置步骤: 1)设置workspace工作间编码: ...
- IDEA 笔记汇总
Intellij IDEA 像eclipse那样给maven添加依赖 Intellij idea maven 引用无法搜索远程仓库的解决方案 Intellij IDEA 封装Jar包(提示错误: 找不 ...
- 查看APK包签名的方法。
1.查看 keystore $ keytool -list -keystore debug.keystore 结果: Keystore type: JKS Keystore provider: SUN ...
- postgresql客户端连接错误的解决方法【转】
今天在重新设置postgresql服务器以后却发现启动不了服务器.错误如下:psql: could not connect to server: No such file or directory ...
- eclipse中怎样添加项目至SVN资源库
转自:https://jingyan.baidu.com/article/642c9d341caac0644a46f73e.html 这是一个SVN最基本的一个使用方法,转一篇别人写的,方便日后查询. ...
- VS生成Map文件
一.右键项目属性->链接器->启用增量链接:关闭,选择 否 (/INCREMENTAL:NO) 二.右键项目属性->链接器->调试-> 生成调试信息:是 (/DEBUG ...
- h2数据库的简单使用
1.登录H2数据库的WebConsole控制台 2.设置数据库连接 3.连接测试通过之后,点击[连接]按钮,登录到test数据库的webConsole 4.创建表 复制H2数据库提供的样例SQL脚本, ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- manjaro 清理系统
sudo pacman -Rsn $(pacman -Qdtq) sudo pacman -Scc sudo rm /var/lib/systemd/coredump/. sudo journalct ...
- redis中save和bgsave区别
转自:redis中save和bgsave区别 SAVE 和 BGSAVE 两个命令都会调用 rdbSave 函数,但它们调用的方式各有不同: SAVE 直接调用 rdbSave ,阻塞 Redis 主 ...