LeetCode_443. String Compression
443. String Compression
Given an array of characters, compress it in-place.
The length after compression must always be smaller than or equal to the original array.
Every element of the array should be a character (not int) of length 1.
After you are done modifying the input array in-place, return the new length of the array.
Follow up:
Could you solve it using only O(1) extra space?
Example 1:
Input:
["a","a","b","b","c","c","c"] Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
Example 2:
Input:
["a"] Output:
Return 1, and the first 1 characters of the input array should be: ["a"] Explanation:
Nothing is replaced.
Example 3:
Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"] Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"]. Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it's own entry in the array.
Note:
- All characters have an ASCII value in
[35, 126]. 1 <= len(chars) <= 1000.
package leetcode.easy;
public class StringCompression {
public int compress(char[] chars) {
int indexAns = 0, index = 0;
while (index < chars.length) {
char currentChar = chars[index];
int count = 0;
while (index < chars.length && chars[index] == currentChar) {
index++;
count++;
}
chars[indexAns] = currentChar;
indexAns++;
if (count != 1) {
for (char c : String.valueOf(count).toCharArray()) {
chars[indexAns] = c;
indexAns++;
}
}
}
return indexAns;
}
@org.junit.Test
public void test() {
char[] chars1 = { 'a', 'a', 'b', 'b', 'c', 'c', 'c' };
char[] chars2 = { 'a' };
char[] chars3 = { 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b' };
System.out.println(compress(chars1));
System.out.println(compress(chars2));
System.out.println(compress(chars3));
}
}
LeetCode_443. String Compression的更多相关文章
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- UVA 1351 十三 String Compression
String Compression Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- CF825F String Compression 解题报告
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- Codeforces 825F - String Compression
825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...
- 区间DP UVA 1351 String Compression
题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...
随机推荐
- Gerrit代码审计系统实战-Gerrit 2.15.14版本快速搭建
Gerrit代码审计系统实战-Gerrit 2.15.14版本快速搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Gerrit版本选择 1>.查看Gerrit官网 ...
- Python入门篇-返回值和作用域
Python入门篇-返回值和作用域 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.返回值 1>.返回值概述 Python函数使用return语句返回“返回值” 所有函数都 ...
- python3爬虫--反爬虫应对机制
python3爬虫--反爬虫应对机制 内容来源于: Python3网络爬虫开发实战: 网络爬虫教程(python2): 前言: 反爬虫更多是一种攻防战,针对网站的反爬虫处理来采取对应的应对机制,一般需 ...
- 深入详解JVM内存模型
JVM内存结构 由上图可以清楚的看到JVM的内存空间分为3大部分: 堆内存 方法区 栈内存 其中栈内存可以再细分为java虚拟机栈和本地方法栈,堆内存可以划分为新生代和老年代,新生代中还可以再次划分为 ...
- scala 中的匹配模式
unapply 仅作匹配,不作其它输出.返回 Boolean 值 object UpperCase { def unapply(s: String): Boolean = s.toUpperCase ...
- 日常SQL总结
THEN '理财帐户' ELSE '其他' end case后可以加入条件在写when,length(String) 为string的字符长度 length(),括号里不仅可以是string的字符串, ...
- SQL操作Spark SQL--BasicSQLTestt
object BasicSQLTest { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .a ...
- MATLAB中运算符优先级
下述运算符的优先级从低到高: 1.先决或(||): 2.先决与(&&): 3.逻辑或(|): 4.逻辑与(&): 5.等于类(<,<=,>,>=,==, ...
- 洛谷P3147 262144
题目 此题数据范围小的话可以用区间\(DP\),但是该题目的数据范围并不能用区间DP来求解,因此我们考虑优化\(DP\). 每个数的生成一定是由这两个区间 考虑区间DP的弊端是并不知道每个数生成的区间 ...
- Java int 与 Integer 区别
学习借鉴(其实搬了别人的好多)和自己的理解,可能会有较多错误,如有疑问联系我呀. int 是基本数据类型, Integer 是引用类型,也就是一个对象. int 储存的是数值,Integer 储存的 ...