[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 ...
随机推荐
- sql拆分列 时间拆分 日、月、年
我想 查看今日访问人数 需要分组查询 就得 时间拆分 这两天百度 方法有很多 substring ... 但是 我这一列 是时间类型: 可以直接用 DATEPART() 函数用于返回日期/时间的单独部 ...
- Vue 获取时间戳返回自定义时间格式
直接在Vue全局函数定义: Vue.prototype.padLeftZero = function(str) { return ('00' + str).substr(str.length); }; ...
- POJ-3262 贪心的一个小技巧
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3204 Accepted: ...
- 如何通过C语言获取主机WLAN下的IPv4地址,MAC地址
#include "stdio.h" #include "windows.h" void GetHostWLAN_IPv4_AND_MAC(char IPv4[ ...
- Python多线程,线程死锁及解决,生产者与消费者问题
1.Thread类 普通调用 t = Thread(target=test, args=(i,)) # test为目标函数名, 若函数需要参数将其以元组形 # 式赋给args, 若无参数可不写 t.s ...
- 2020/2/6 PHP编程学习
今天把后台数据库处理好了,用了框架后真就是搬砖的一天..晚上继续刷题,明天把数据库处理完,这样一个商城框架就有了:
- Linux简介和环境的搭建
Linux的学习方向 网络服务器 嵌入式程序开发 Linux的设计哲学:一切皆文件 常用命令:cd 切换目录sudo shutdown -h now 关机命令sudo reboot 重启sudo ro ...
- Tensorflow学习教程------Fetch and Feed
#coding:utf-8 import tensorflow as tf #Fetch input1 = tf.constant(3.0) input2 = tf.constant(1.0) inp ...
- Java四类八种
四类: 整数类型,浮点类型,字符型,布尔型 八种: byte,short,int,long,float,double,char,boolean
- Causal Corpus 事件因果关系语料统计
Causal Corpus 事件因果关系语料统计 本文是对因果关系抽取领域数据库标注及开源情况的统计.除了对因果关系的标注,一些类似的语料也包含在内,从而为语料的使用提供灵活性,可以根据不同的目标选取 ...