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();
}
}

最新文章

  1. Save()saveOrUpdate()Hibernate的merge()方法
  2. Ecplise真机调试(Android)
  3. 【poj1018】 Communication System
  4. git 创建本地分支,然后推送到服务器上
  5. sencha treestore 取消自动加载数据
  6. PHP数组的排序函数
  7. SQL Server 2014 64位版本链接32位Oracle数据库
  8. An Easy Problem?!(细节题,要把所有情况考虑到)
  9. css21规范学习
  10. 提取Word里的文本内容 C#
  11. proposal-cancelable-promises
  12. QT listwiget 控件添加图片
  13. poj 3177 Redundant Paths(边双连通分量+缩点)
  14. Micro QR 和QR码
  15. java.sql.SQLException: Value &#39;0000-00-00 00:00:00&#39; can not be represented as java.sql.Date
  16. org.json
  17. E. Turn Off The TV Educational Codeforces Round 29
  18. ElastAlert规则
  19. Rails Guide--Working with JavaScript in Rails; 如何把jquery转化为原生js
  20. c# 调用外包程序 等待处理完成结果

热门文章

  1. CNN核心概念理解
  2. 虚拟机vmware vmnet8 未识别(转)
  3. QSignalMapper is deprecated
  4. 简单的说一下react路由(逆战班)
  5. Python String startswith() Method
  6. Idea 中的快捷键(mac)
  7. CodeForces 51C 二分搜索
  8. Java学习——代理模式
  9. 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单验证
  10. SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&amp;2-3 逻辑运算符