原题链接在这里: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. duff's device

    const duffDevice = (items, process) => { let iterations = Math.floor(items.length / 8); let start ...

  2. 【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件

    本文主要介绍paramiko远程执行linux命令,及在服务器上进行文件的上传.下载 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. ...

  3. mongodb禁止外网访问以及添加账号

    未曾料到被黑客勒索比特币的戏码竟然降临到我的身上,几个月的技术积累付之一炬.怪只怪自己学艺不精,心存侥幸和无知,不过经此一役,方知网络安全防护的重要性. 一直未给自己的mongodb数据库设置账号密码 ...

  4. 利用python 模块读取csv文件信息

    还有一个比较简单的方法 # -*- coding=utf-8 -*- import pandas as pddf = pd.read_csv("20170320094630.csv" ...

  5. Zookeeper在 Linux 系统的安装

    安装步骤:第一步:安装 jdk第二步:把 zookeeper 的压缩包上传到 linux 系统.Alt+P 进入 SFTP ,输入 put d:\zookeeper-3.4.6.tar.gz 上传第三 ...

  6. 转:在Eclipse的Debug页签中设置虚拟机参数

    http://blog.csdn.net/decorator2015/article/details/50914479 在Eclipse的Debug页签中设置虚拟机参数 步骤 1,Run->De ...

  7. MYSQL中的日期转换

    MYSQL中的日期转换 网址: http://www.eygle.com/digest/2006/09/mysql_date_convert.html 对于每个类型拥有的值范围以及并且指定日期何时间值 ...

  8. COW写时复制

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  9. MAC 下安装 SVN

    在mac下安装svn走了很多弯路,进过探索,现在对svn的安装做了总结,可以分为2种: 1.由于 xCode自带svn,所以可以安装xCode 1).打开App Store,搜索xCode,下载安装 ...

  10. 添加courses模块

    startapp courses from django.db import models from datetime import datetime # Create your models her ...