原题链接在这里: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. ItemsControl的ItemContainerStyle属性

    ItemsControl:ListBox,ComboBox,TreeView ItemContainerStyle是用来设置每一个集合控件的Item的样式的属性(即设置每一个项的样式).   使用It ...

  2. 大小堆C++实现

    C++大小堆实现(仿函数) 具体代码如下 #pragma once #include<iostream> #include<vector> using namespace st ...

  3. R读取一个数据框 Dataframe,删去其中的某一列

    可以参考:http://blog.sina.com.cn/s/blog_80572f5d0101anxw.html

  4. Ansible 小手册系列 十三(Jinja2)

    用于playbook中的jinja 2过滤器 更改数据格式,其结果是字符串 {{ some_variable | to_json }} {{ some_variable | to_yaml }} 对于 ...

  5. 【hive】多表插入

    from or_table insert overwrite table1 name1 select … insert into table2 name2 select … 注意:select 后边不 ...

  6. css居中方法详解

    水平居中: 通过设置父元素,让子元素内容居中:text-align:center; 通过设置子元素本身,让子元素居中:margin:0 auto; 以上方法生效的前提条件是子元素没有被float元素影 ...

  7. 将封装了envi功能的IDL类导出成java类,方便java调用

    目的:     用IDL将ENVI的功能封装成为IDL的类,并使用IDL的对象导出功能把这些功能类导出为java类,方便java调用.(本来想直接通过GP工具调用的,但是没有授权文件)   操作步骤: ...

  8. js自定义对象.属性 笔记

    <一> js自定义对象 一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtabl ...

  9. Server.Transfer 页面之间传值

    server.transfer 特点: 1:大家熟悉的一个特点,用server.transfer 跳转到新页面时,浏览器的地址是没有改变的(因为重定向完全在服务器端进行,浏览器根本不知道服务器已经执行 ...

  10. XML方式实现Spring的AOP

    1.编写切面类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.fz.an ...