[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 ...
随机推荐
- MySQL的优化与执行
MySQL会解析查询,并创建内部数据结构(解析树),然后对其进行各种优化,包括重写查询.决定表的读取顺序,以及选择合适的索引等.用户可以通过特殊的关键字提示(hint)优化器,影响它的决策过程.也可以 ...
- DevOps专题 |监控,可观测性与数据存储
对于DevOps而言,监控是其中重要的一环,上一次的专题内容中,我们与大家分享了大型企业级监控系统的设计.今天我们将和大家从另一个角度进一步探讨互联网工程技术领域的监控设计(monitoring):系 ...
- STM32F407的Modbus做为主站与从站通讯
在调试STM32F407的串口Modbus通讯之前,也使用过Modbus通讯,只不过都是在PLC或则昆仑通态的触摸屏上使用直接调用现成的库里面的模块,驱动就可以,相对于STM32来,使用PLC库里面的 ...
- matlab中画一条折线时怎样显示出每个点折点的数值
举个例子: num=[5,44,62,154,264,365,398,480,619,705,762,728,669,726,890,731,707,696,558,509,444];date=[1. ...
- Webstorm、Idea双击shift弹出框解决办法
1.Ctrl + Shift + A,输入registry 2.在弹出的记录表中,向下滚动到**“ide.suppress.double.click.handler”**并选中复选框,然后close关 ...
- 基于迅为-i.MX6Q开发板制作镜像方法
在“/home/imx6”目录(在前面编译 android 系统的时候新建过这个目录,如果没有可以自己新建一个)下,使用命令“mkdir minilinux/”新建 minilinux 目录,使用命令 ...
- VuePress 中增加用户登录功能
在 VuePress 中增加用户登录 VuePress 是 Vuejs 官方提供的一个快速建设文档站点的工具,在简单配置好功能后,需要做的事情就剩下写好一个个 Markdown 文档. 因为 VueP ...
- 201409-2 画图 Java
思路: 法1:计算每个矩形的小方块,去掉重复的 法2:二维数组,需要涂色就置flag为1,最后遍历输出,不会有重复计算 import java.util.Scanner; public class M ...
- Unity3D事件函数的执行顺序
In Unity scripting, there are a number of event functions that get executed in a predetermined order ...
- switch-case的用法
case 值1: 表达式的值和 值1匹配上了,需要执行的代码; break; case 值2: 表达式的值和 值2匹配上了,需要执行的代码; break; case 值3: 表达式的值和 值3匹配上了 ...