[Algo] 175. Decompress String II
Given a string in compressed form, decompress it to the original string. The adjacent repeated characters in the original string are compressed to have the character followed by the number of repeated occurrences.
Assumptions
The string is not null
The characters used in the original string are guaranteed to be ‘a’ - ‘z’
There are no adjacent repeated characters with length > 9
Examples
“a1c0b2c4” → “abbcccc”
public class Solution {
public String decompress(String input) {
// Write your solution here
if (input.length() == 0) {
return input;
}
StringBuilder sb = new StringBuilder();
int i = 0;
char[] charArr = input.toCharArray();
char prevChar = input.charAt(0);
while (i < charArr.length) {
char cur = charArr[i];
if (Character.isLetter(cur)) {
prevChar = cur;
i += 1;
} else if (Character.isDigit(cur)) {
int num = cur - '0';
if (num == 0) {
i += 1;
} else {
while (i + 1 < charArr.length && Character.isDigit(charArr[i + 1])) {
num = 10 * num + (charArr[i + 1] - '0');
i += 1;
}
for (int j = 0; j < num; j++) {
sb.append(prevChar);
}
i += 1;
}
}
}
return sb.toString();
}
}
public class Solution {
public String decompress(String input) {
// Write your solution here
char[] charArr = input.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArr.length; i++) {
char cur = charArr[i++];
int num = charArr[i] - '0';
for (int j = 0; j < num; j++) {
sb.append(cur);
}
}
return sb.toString();
}
}
[Algo] 175. Decompress String II的更多相关文章
- [Algo] 611. Compress String II
Given a string, replace adjacent, repeated characters with the character followed by the number of r ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- Reverse Words in a String I & Reverse Words in a String II
Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- LeetCode 541. 反转字符串 II(Reverse String II)
541. 反转字符串 II 541. Reverse String II
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
- Compress and decompress string
You are given a string with lower case letters only. Compress it by putting the count of the letter ...
- 186. Reverse Words in a String II
题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...
随机推荐
- 大二暑假第三周总结--开始学习Hadoop基础(二)
简单学习NoSQL数据库理论知识 NoSQL数据库具有以下几个特点: 1.灵活的可扩展性(支持在多个节点上进行水平扩张) 2.灵活的数据模型(与关系数据库中严格的关系模型相反,显得较为松散) 3.与与 ...
- C语言-逃逸字符、类型转换和布尔类型
C语言-逃逸字符 逃逸字符是用来表达无法印出来的控制字符或者特殊字符,它由一个反斜杠""开头,后面跟上另一个字符,这两个字符合起来,组成一个字符. \b是backspace,在su ...
- 合理控制MBA Essay写作字数很重要
作为一个MBA申请人,在Essay写作的时候一定会迸发各种各样的想法和念头,想要统统传达给招生官.然而面对有限的字数限制,想要尽可能多地在Essay中涵盖重要信息,就让人颇为头痛了. 面对想要倾诉的欲 ...
- redis常用命令--strings
strings常用命令: set key value:存值 get key:取值 append key value:在值后追加值 strlen key:获取长度 mset key1 value1 ke ...
- Windb实践之Script Command
1.输出参数 .echo The first argument is ${$arg1}. .echo The fifth argument is ${$arg5}. .echo The fourth ...
- Jackknife,Bootstrap, Bagging, Boosting, AdaBoost, RandomForest 和 Gradient Boosting的区别
Bootstraping: 名字来自成语“pull up by your own bootstraps”,意思是依靠你自己的资源,称为自助法,它是一种有放回的抽样方法,它是非参数统计中一种重要的估计统 ...
- D语言-运算符
Part 0:概念 表达式:表达式是由非赋值运算符或特殊运算符和值组成的,每个表达式都可以计算出一个值 Part 1:非赋值运算符 §1.1 基本的运算符 基本的运算符有+,-,*,/,% 我相信你除 ...
- preg_match()//explode()// 输出函数//assert()//trim()
1.preg_match() 这是一个正则表达式匹配的函数,其用法如下: 查找文本字符串"php": <?php //模式分隔符后的"i"标记这是一个大小 ...
- HTML 回到顶部 浮动
回到顶部 <div id="FloatDIV" style="position: absolute; top: 0px; z-index: 9999; backgr ...
- css中标签总结
cursor CSS属性定义鼠标指针悬浮在元素上方显示的鼠标光标cursor:pointer: 小手 cursor:wait:等待....很多种 <span contenteditable=&q ...