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的更多相关文章

  1. [Algo] 611. Compress String II

    Given a string, replace adjacent, repeated characters with the character followed by the number of r ...

  2. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. 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 ...

  5. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  6. LeetCode 541. 反转字符串 II(Reverse String II)

    541. 反转字符串 II 541. Reverse String II

  7. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

  8. Compress and decompress string

    You are given a string with lower case letters only. Compress it by putting the count of the letter ...

  9. 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 ...

随机推荐

  1. ES6 - 装饰器 - Decorater

        注意,修饰器对类的行为的改变,是代码编译时发生的,而不是在运行时.这意味着,修饰器能在编译阶段运行代码.也就是说,修饰器本质就是编译时执行的函数.   修饰器是一个对类进行处理的函数.修饰器函 ...

  2. python奇淫技巧之 抽屉 自动点赞

    前言 嘿,各位小伙伴晚上好呀,今天又给大家带来干货内容啦,今天带来的是,如何自动登录抽屉,并且点赞 原计划打算,是不打算使用selenium的,但是因为要涉及点赞,所以免不了登录,但是我又被啪啪打脸了 ...

  3. ssh and scp从远程服务器下载文件

    scp -r root@172.16.252.32:/home/files /home/files   下载目录  -r root是用户172.16.252.32是ip:/home/files 是你要 ...

  4. MVC三层架构在各框架中的特征

    转一篇写得很棒的文章:https://my.oschina.net/win199176/blog/208171?p=7&temp=1495894148424 1.基于web开发中最原始的jsp ...

  5. io流对数据的读写

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...

  6. 【数据结构】C++语言环形队列的实现

    队列--先进先出 队列的一个缺点--出队后的内存空间浪费了,不能二次利用 环形队列--解决以上缺点的队列,用过的内存空间可以重复利用 github: https://github.com/HITFis ...

  7. 吴裕雄--天生自然 PHP开发学习:运算符

    <?php $x=10; $y=6; echo ($x + $y); // 输出16 echo '<br>'; // 换行 echo ($x - $y); // 输出4 echo ' ...

  8. Android群英传神兵利器读书笔记——第一章:程序员小窝——搭建高效的开发环境

    1.1 搭建高效的开发环境之操作系统 1.2 搭建开发环境之高效配置 基本环境配置 基本开发工具 1.3 搭建程序员的博客平台 开发者为什么要写作 写作平台 第三方博客平台 自建博客平台 开发论坛 1 ...

  9. [极客大挑战 2019]FinalSQL

    0x00 知识点 盲注 0x01 解题 根据题目提示盲注,随便点几下找到注入点 发现我们输入^符号成功跳转页面,证明存在注入 1^(ord(substr((select(group_concat(sc ...

  10. 身边的人工智能&人工智能发展史

    智能家具 扫地机器人 智能音箱 个人助手 在线翻译 谷歌翻译 微软翻译 YouTube 视频翻译 图像识别 人脸识别 AI+摄像头 下棋高手 Alphago 2017年打败柯洁 成为世界第一 Alph ...