原题链接在这里:https://leetcode.com/problems/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.

题解:

Accumlate the count of repeating chars, if count > 1, append to the char.

Use (""+count).toCharArray() to easily append int as char array.

Time Complexity: O(chars.length). Space: O(1).

AC Java:

 class Solution {
public int compress(char[] chars) {
if(chars == null || chars.length == 0){
return 0;
} int pos = 0;
int count = 0;
int i = 0;
while(i<chars.length){
char cur = chars[i];
while(i<chars.length && chars[i] == cur){
count++;
i++;
} chars[pos++] = chars[i-1];
if(count > 1){
for(char c : (""+count).toCharArray()){
chars[pos++] = c;
}
} count = 0;
} return pos;
}
}

类似Encode and Decode StringsCount and SayDesign Compressed String Iterator.

LeetCode String Compression的更多相关文章

  1. [LeetCode] String Compression 字符串压缩

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  2. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

  3. 443. String Compression - LeetCode

    Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...

  4. LeetCode_443. String Compression

    443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...

  5. UVA 1351 十三 String Compression

    String Compression Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  6. 443. String Compression

    原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...

  7. CF825F String Compression 解题报告

    CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...

  8. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  9. 213. String Compression【easy】

    Implement a method to perform basic string compression using the counts of repeated characters. For ...

随机推荐

  1. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  2. 最优化问题 Optimization Problems & 动态规划 Dynamic Programming

    2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...

  3. Maven 三种archetype说明

    新建Maven project项目时,需要选择archetype. 那么,什么是archetype? archetype的意思就是模板原型的意思,原型是一个Maven项目模板工具包.一个原型被定义为从 ...

  4. torch7 调用caffe model 作为pretrain

    torch7 调用caffe model 作为pretrain torch7 caffe preTrain model zoo torch7 通过 loadcaffe 包,可以调用caffe训练得到的 ...

  5. http协议code码

    301 永久重定向 类似手机呼叫转移 302 临时重定向 类似手机呼叫转移 403 forbidden ngnix怎么解决? 含义:表示你在请求一个资源文件,但是nginx不允许你查看. 原因1:访问 ...

  6. java程序设计基础篇 复习笔记 第一单元

    java语言程序设计基础篇笔记1. 几种有名的语言COBOL:商业应用FORTRAN:数学运算BASIC:易学易用Visual Basic,Delphi:图形用户界面C:汇编语言的强大功能和易学性,可 ...

  7. centos静默安装oracle12c

    配置系统和安装所需软件包 关闭selinux 临时关闭(不用重启) [root@SVR-3-125 ~]# setenforce 0 修改配置文件(需要重启):  将SELINUX=enforcing ...

  8. bzoj1711

    题解: 原点->食物建一个1 食物->牛见一个1 牛->牛'见一个1 牛'->饮料1 饮料->汇点1 代码: #include<cstdio> #includ ...

  9. vue浏览器返回监听

    具体步骤如下: 1.挂载完成后,判断浏览器是否支持popstate mounted(){ if (window.history && window.history.pushState) ...

  10. PHP stripos()、strripos()和strrpos() 使用方法和区别

    区别 stripos():查找字符串首次出现的位置(不区分大小写) 写法:stripos ( string $haystack , string $needle [, int $offset = 0 ...