443. String Compression

Easy

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:

  1. All characters have an ASCII value in [35, 126].
  2. 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的更多相关文章

  1. 【leetcode】443. String Compression

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

  2. UVA 1351 十三 String Compression

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

  3. 443. String Compression

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

  4. CF825F String Compression 解题报告

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

  5. 213. String Compression【LintCode java】

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

  6. 213. String Compression【easy】

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

  7. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  8. Codeforces 825F - String Compression

    825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...

  9. 区间DP UVA 1351 String Compression

    题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...

随机推荐

  1. 2019年牛客多校第二场 H题Second Large Rectangle

    题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...

  2. 解决SELinux导致Apache更改端口后无法启动的问题

    systemctl start httpd    # 将Apache的默认端口改为90后,启动Apache时提示失败 systemctl status httpd    # 查看Apache的状态 可 ...

  3. 移动平台前端开发总结(ios,Android)

    首先我们来看看webkit内核中的一些私有的meta标签,这些meta标签在开发webapp时起到非常重要的作用 <meta content="width=device-width; ...

  4. P5024 保卫王国[倍增+dp]

    窝当然不会ddp啦,要写这题当然是考虑优化裸dp啦,但是这题非常麻烦,于是变成了黑题. 首先,这个是没有上司的舞会模型,求图的带权最大独立集. 不考虑国王的限制条件,有 \[ dp[x][0]+=dp ...

  5. (转)python自动化测试之异常及日志

    为了保持自动化测试用例的健壮性,异常的捕获及处理,日志的记录对掌握自动化测试执行情况尤为重要,这里便详细的介绍下在自动化测试中使用到的异常及日志,并介绍其详细的用法. 一.日志 打印日志是很多程序的重 ...

  6. django 进行语言的国际化及在后台进行中英文切换

    项目的部署地为: 中国大陆与美国东海岸, 两个地区的服务器数据不进行同步, 中国地区的服务器页面展示中文, 美国地区的服务器页面展示成英文, 项目后台使用python编程语言进行开发, 并结合djan ...

  7. git基本操作及设置

    $ git config --global user.name "wstmljf" $ git config --global user.email "wstmljf@1 ...

  8. prisma mongodb 试用

    prisma 已经支持mongodb了,我们需要做的就是安装新版本的prisma cli,后然初始化项目使用 环境准备 安装cli 注意使用新版本(prisma/1.32.2) 低版本有坑 npm i ...

  9. snmp-get

    使用mibbroser可以连接到监控主机,可以获取主机mib信息 使用walk出的oid就可以获取到对应的值, 使用 -O fn 可以将返回的字符创形式的键改为数字型oid oid还有一种字符串的形式 ...

  10. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...