原题链接在这里: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. 构建hadoop集群时遇到的问题

    在构建hadoop集群时,出现过主节点中的namenode或datanode启动不成功的问题.在日志文件中往往会显示namenode和datanode中clusterID不相同的问题,这个问题往往都是 ...

  2. 常见ADB命令

    常见ADB命令 比如说知道了push和pull操作,就可以实现一个简单的手机助手. 如果有多台设备,操作的时候要指定设备.  -s加设备名称

  3. gem doorkeeper(4000✨) ,Go-rails视频

    博客OAuth教程:https://i.cnblogs.com/EditPosts.aspx?postid=9531091 doorkeeper: (4000

  4. opencv 图片降噪

    —— # -*- coding: utf-8 -* import numpy as np import cv2 cap = cv2.VideoCapture(0) while True: _ , fr ...

  5. confluence wiki 破解安装操作流程

    准备postgres数据库安装 步骤1:命令: docker pull postgres 步骤2:安装: docker run --name postgresdb -p 5432:5432 -e PO ...

  6. 【Python】改变对象的字符串显示

    问题 改变对象实例的打印或显示输出,让它们更具可读性. 解决方案 要改变一个实例的字符串表示,可重新定义它的 __str__() 和 __repr__() 方法.例如: class Pair: def ...

  7. javascript打开新窗体

    open -- 打开(弹出)一个新的窗体 open,中文"打开"的意思 引用网址:http://www.dreamdu.com/javascript/window.open Jav ...

  8. 008-对象—— 对象$this self parent 内存方式及使用方法讲解

    <?php /** * */ /*class Web{ private $webname; private $weburl; function __construct($webname,$web ...

  9. SqlServer 转 Oracle 的几点注意

    (转自:http://www.2cto.com/database/201208/146740.html) 1.字符型的字段相加需要用“||”,如果用“+”的话,会报“无效的数字”的错误.   2.类似 ...

  10. HDU 5875 Function (线段树+gcd / 单调栈)

    题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对数字b取模时:如果a<b,则等于原数,否则a会变小至少一半. ...