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. 由于找不到msvcp100.dll无法继续执行代码

    最近重装系统之后安装mysql, 执行 mysqld install 命令时出现 : 由于找不到msvcp100.dll无法继续执行代码... 解决办法 下载 Microsoft Visual C++ ...

  2. cf 507E. Breaking Good

    因为要求是在保证最短路的情况下花费是最小的,所以(先保证最短路设为S吧) 那么花费就是最短路上的新建边条数A+剩余拆掉边的条数B,而且总的原有好的边是一定的,所以,只要使得A尽量小,那么B就大,所以要 ...

  3. DFS+BFS(广度优先搜索弥补深度优先搜索遍历漏洞求合格条件总数)--09--DFS+BFS--蓝桥杯剪邮票

    题目描述 如下图, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)  比如,下面两张图中,粉红色所示部分就是合格的剪取.  请你计算,一共有多少 ...

  4. ucosiii 学习笔记

    OSTaskCreate 的参数中有一个 void *p_ext,这个参数用来传递额外的信息,可以用来传递数组,结构体等等. AppTaskStart --> CPU_Init --> C ...

  5. centos7 lvm合并分区脚本初探-linux性能测试 -centos7修改网卡名字-jdk环境安装脚本-关键字查询文件-批量添加用户

    1.#!/bin/bash lvmdiskscan | grep centos > /root/a.txt a=`sed -n '1p' /root/a.txt` b=`sed -n '2p' ...

  6. 一、VIP课程:互联网工程专题 01-Git基本概念与核心命令掌握

    第一课:Git基本概念与核心命令掌握.docx 课程概要: GIT 体系概述 GIT 核心命令使用 GIT 底层原理 一.GIT体系概述 1.使用方式区别 从本地把文件推送远程服务,SVN只需要com ...

  7. Servlet详细教程

    Servlet简介 servlet是Server Applet的简称,翻译过来就是服务程序.好吧,这么说你可能还是不太懂,简单的讲,这个servlet是运行在服务器上的一个小程序,用来处理服务器请求的 ...

  8. 1-4 无监督学习(Unsupervised Learning)

    无监督学习定义: [无监督学习]中没有任何的标签或者是有相同的标签或者就是没标签.所以我们已知数据集,却不知如何处理,也未告知每个数据点是什么.别的都不知道,就是一个数据集.你能从数据中找到某种结构吗 ...

  9. (4)关于Alpha通道问题

    其实,我还是不理解,我还是先把我目前懂得和觉得有用的东西先存下来 =================================================================== ...

  10. linux下的hashpump安装

    hashpump是linux上的一个进行hash长度拓展攻击的工具 安装: git clone https://github.com/bwall/HashPump apt-get install g+ ...