[LC] 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".
class Solution {
public int compress(char[] chars) {
if (chars.length <= 1) {
return chars.length;
}
int index = 0;
int res = 0;
while (index < chars.length) {
int curCount = 0;
char curChar = chars[index];
while (index < chars.length && chars[index] == curChar) {
index += 1;
curCount += 1;
}
chars[res++] = curChar;
if (curCount != 1) {
for (char c: Integer.toString(curCount).toCharArray()) {
chars[res] = c;
res += 1;
}
}
}
return res;
}
}
[LC] 443. String Compression的更多相关文章
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- 443. String Compression - LeetCode
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...
- 443. String Compression字符串压缩
[抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...
- leetcode 443. String Compression
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...
- LeetCode 443. String Compression (压缩字符串)
题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- LeetCode_443. String Compression
443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...
随机推荐
- Linq------连表查询
1 List<Student> list = new List<Student>() { ,sex="男"}, ,sex="男"}, , ...
- vivado下创建基本时序周期约束
创建基本时钟周期约束.(验证我们的设计能否在期望的频率上运行) (学习记录,晚一点会做实验传上来的.) 时钟基本概念:https://blog.csdn.net/wordwarwordwar/arti ...
- Bugku 加密(持续更新)
1.滴答~滴 不多说,摩斯密码解密. 2.聪明的小羊 栅栏密码解密. 3.ok Ook解密 4.这不是摩斯密码 brainfuck解码 5.简单加密 凯撒有两种编码脚本,一种是字母26内循环移位,一种 ...
- 移植zlib
平台说明 开发平台:Ubuntu12.04 编 译器:arm-linux-gcc version 4.4.4 (4.4.4_09.06.2010) Zlib源码包:zlib-1.2.11.tar.gz ...
- 合并两个django的queryset
有queryset:A和B 要合并它们,根据网上的答案,貌似是用itertools库的chain对象比较好,地址 c=chain(x,y) 但是当c用于分页的时候,就有问题,会报chain没有le ...
- Excel Old format or invalid type library 错误原因
Old format or invalid type library 错误原因 调用excel方法失败,Old format or invalid type library 解决方案: 1,这是Exc ...
- 201412-2 Z字形扫描 Java
思路: 观察输出可以发现,可以不用定义 "方向" ,看斜线,如果是第偶数条(0也是偶数),从左下到右上输出.如果是第奇数条,从右上到左下输出. import java.util.S ...
- 17.3.10--->关于数值溢出问题
取值范围: short.int.long 占用的字节数不同,所能表示的数值范围也不同.以32位平台为例,下面是它们的取值范围: 数据类型 所占字 ...
- Anaconda环境安装
Anaconda环境安装 一.Anaconda Anaconda是Python的一个开源的发行版本,里面包含了很多科学计算相关的包,它和Python的关系就像linux系统中centos和Ubuntu ...
- oracle中带参存储过程的使用
Oracle中存储过程带参分为:输入参数(in)和输出参数(out) 例如: create or replace procedure out_test(v_user in emp.user_name% ...