LeetCode_443. String Compression
443. 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.
Note:
- All characters have an ASCII value in
[35, 126]
. 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的更多相关文章
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- UVA 1351 十三 String Compression
String Compression Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- CF825F String Compression 解题报告
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- Codeforces 825F - String Compression
825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...
- 区间DP UVA 1351 String Compression
题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...
随机推荐
- 如何为SUSE配置IP地址,网关和DNS
方法一.在命令行中配置.输入: ifconfig eht0 9.111.66.96 netmask 255.255.255.0 up route add default gw 9.111.66.1 方 ...
- Python开发应用之-SQL 建索引的几大原则
SQL 建索引的几大原则: 最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹配,比如a = 1 and b = ...
- 20199301《Linux内核原理与分析》第十一周作业
Linux Capability探索实验 一.实验描述 本实验中,将感受到linux capability功能在访问控制上的优势,掌握使用Capability达到遵守最小权限原则的目的,并分析linu ...
- IntelliJ IDEA 查找两个字符之间任意内容正则表达式
表达式: A.*?B(“.“表示任意字符,“?”表示匹配0个或多个)
- python(三)——while语句
while死循环 #!/usr/bin/env python #-*- coding:utf8 -*- import time while 1 == 1: print('Ok',time.time() ...
- Chocolatey 方便的windows 包管理工具
windows 在包管理上一般大家都是网上下载二进制文件或者就是通过软件管家进行安装,这些对于开发人员可能就有点不是 很专业了, Chocolatey 是一个不错的windows 软件包管理工具 安装 ...
- 61、Spark Streaming:部署、升级和监控应用程序
一.部署应用程序 1.流程 1.有一个集群资源管理器,比如standalone模式下的Spark集群,Yarn模式下的Yarn集群等. 2.打包应用程序为一个jar包. 3.为executor配置充足 ...
- GoCN每日新闻(2019-10-03)
GoCN每日新闻(2019-10-03) 国庆专辑:GopherChina祝大家国庆节快乐 GoCN每日新闻(2019-10-03) 1. 垃圾回收器如何监控你的应用程序 https://medium ...
- (16)Go文件处理
package main import ( "bufio" "fmt" "os" ) func main() { // 新建文件 file, ...
- MAKEFILE编写学习--1
makefile是在编译中大型程序中使用的自动化编译工具make依赖的指令文件.这样可以使得程序的编译更加便捷快速. makefile的一般规则如下: target ... : prerequisit ...