[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 ...
随机推荐
- Q3狂揽3亿美元净利润的特斯拉会让国内电动汽车厂商喜极而泣吗?
作为电动汽车行业的标杆,特斯拉无疑是国内电动汽车厂商发展进程中重要的参考对象.而前段时间特斯拉身上出现的产能受阻.私有化风波.马斯克卸任董事长一职等事件,着实让国产电动汽车厂商惊出一身冷汗.毕竟如果特 ...
- ORACLE 将一个库的部分值带条件插入到另外一个库
将一个表插入另外一个表,两种方法: 1.insert into table1 select * from table2 ; 或者2.create table1 as select * from tab ...
- 5 ~ express ~ 连接数据库
1, 在schema 目录创建 users.js 文件,通过 mongoose 模块来操作数据库 2, 在定义 users 表结构之前,需要让应用支持或连接数据库 . 所以要在应用的入口文件 app ...
- 全面掌握Nginx配置+快速搭建高可用架构 一 random_index_module 随机主页
修改default.conf 保存重载Nginx,完成配置 自动随机选择主页 string要替换的内容,replacement表示替换后的内容 示例 效果,只替换了第一个 如果要替换所有的,需要用到s ...
- 如何保障Assignment写作效率?
有没有因为开学要交的Assignment而日夜赶工.身心俱疲啊?写Assignment确实是个体力+脑力活,要一直保持旺盛的精力并不容易.精神和身体的疲劳会慢慢分散你的注意力,进而影响效率和写作质量. ...
- QSignalMapper is deprecated
今天参考 qt4 的书籍,在 qt5 的平台上面,用了 QSignalMapper,结果收到警告" QSignalMapper is deprecated". 经过一番查找,找到了 ...
- MD5碰撞和MD5值(哈希值)相等
md5的碰撞 0e开头的md5和原值: s878926199a 0e545993274517709034328855841020 s155964671a 0e342768416822451524974 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于Annotation装配Bean
在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. Java 从 J ...
- Web报文压缩方法
编译时压缩 https://www.cnblogs.com/qiuzhimutou/p/7592875.html 这里我列举几个常用的能够用于减少包体大小的插件,我们可以根据项目需求选择性的使用: c ...
- promise核心技术 1 实例对象/函数对象
一个程序员要在看到代码的语法同时判断数据类型 知道语法是基础 基础才能延伸功能 //一行代码 a()[0]() // a() 首先推断出a是一个函数 //a()[0] 判断a函数的返回值是一个数组 ...