[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 ...
随机推荐
- setoolkit+花生壳 制作钓鱼网站
国家法律一定要遵守,知识要用在对的地方. 本贴只为了和大家交流学习,请勿用在其他地方,损害任何人的利益. 今天我,来说一下钓鱼网站 (在kali) 我们选择 1 回车 再选择 2 回车 再选择3 ...
- 直击JDD | 京东技术全景图首次展示 四大重磅智能技术驱动产业未来!
11月19日,主题为"突破与裂变"的2019京东全球科技探索者大会(JDDiscovery)在京盛大开幕,京东集团展示了完整的技术布局与先进而丰富的对外技术服务,对外明确诠释了&q ...
- 吴裕雄--天生自然TensorFlow2教程:Broadcasting
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 数据类型
MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准SQL数值数据类型. 这 ...
- Linux--wget,yum,rpm,apt-get
参考:http://blog.csdn.net/ziju125521/article/details/52575715 使用wget下载一个 rpm包, 然后用 rpm -ivh xxx.rpm ...
- 腾讯一shell试题.
腾讯一shell试题. 假设qq.tel文件内容: 12334:13510014336 12345:12334555666 12334:12343453453 12099:13598989899 12 ...
- JAVA中如何判断一个输入是数字(小数和整数)还是字符串?
public class Test1 { public static void main(String[] args) { Scanner input = new Scanne ...
- TensorFlow中的Session、Graph、operation、tensor
TensorFlow中的Session.Graph.operation.tensor
- 用IDLE调试python程序
1. 设置断点 先放例子: import pdb a=1 b=10 pdb.set_trace()#这里是断点 c=a+b print(c) import pdb 后,用pdb.set_trace() ...
- redis 会丢数据吗
不管是以前的主从模式(哨兵模式),还是现在的集群模式,因为都用了slave of 同步; 而slave of 同步会丢弃本地数据,直接用对方的数据来覆盖本地,所以会丢失数据 1.主备网络不通,后续主节 ...