当前位置:网站首页>LeetCode_443_压缩字符串
LeetCode_443_压缩字符串
2022-08-10 10:43:00 【Fitz1318】
题目链接
题目描述
给你一个字符数组 chars
,请使用下述算法压缩:
从一个空字符串 s
开始。对于 chars
中的每组 连续重复字符 :
- 如果这一组长度为
1
,则将字符追加到s
中。 - 否则,需要向
s
追加字符,后跟这一组的长度。
压缩后得到的字符串 s
不应该直接返回 ,需要转储到字符数组 chars
中。需要注意的是,如果组长度为 10
或 10
以上,则在 chars
数组中会被拆分为多个字符。
请在 修改完输入数组后 ,返回该数组的新长度。
你必须设计并实现一个只使用常量额外空间的算法来解决此问题。
示例 1:
输入:chars = ["a","a","b","b","c","c","c"]
输出:返回 6 ,输入数组的前 6 个字符应该是:["a","2","b","2","c","3"]
解释:"aa" 被 "a2" 替代。"bb" 被 "b2" 替代。"ccc" 被 "c3" 替代。
示例 2:
输入:chars = ["a"]
输出:返回 1 ,输入数组的前 1 个字符应该是:["a"]
解释:唯一的组是“a”,它保持未压缩,因为它是一个字符。
示例 3:
输入:chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
输出:返回 4 ,输入数组的前 4 个字符应该是:["a","b","1","2"]。
解释:由于字符 "a" 不重复,所以不会被压缩。"bbbbbbbbbbbb" 被 “b12” 替代。
提示:
1 <= chars.length <= 2000
chars[i]
可以是小写英文字母、大写英文字母、数字或符号
解题思路
双指针法
left
,right
分别为窗口的边界- 每当
right
移动到某一段连续相同子串的最右侧时,我们就在left
处依次写入该字符串对应的字符和子串长度即可- 如果重复子串长度为1,那么
left++
即可 - 如果重复子串长度在[2,9],那么
chars[left + 1] = (char) (num + '0');
,并且left += 2
- 如果重复子串长度在[10,无穷],那么就将长度转换成String,在遍历加入String.
chars[left + 1 + i] = nums.charAt(i);
。并且left += nums.length + 1
- 如果重复子串长度为1,那么
AC代码
class Solution {
public int compress(char[] chars) {
int left = 0;
int right = 0;
while (right < chars.length) {
int index = right;
while (right < chars.length && chars[index] == chars[right]) {
right++;
}
int num = right - index;
chars[left] = chars[right - 1];
if (num == 1) {
left++;
} else if (num <= 9) {
chars[left + 1] = (char) (num + '0');
left += 2;
} else {
String nums = num + "";
for (int i = 0; i < nums.length(); i++) {
chars[left + 1 + i] = nums.charAt(i);
}
left += nums.length() + 1;
}
}
return left;
}
}
边栏推荐
- 商城限时秒杀功能系统
- Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
- 【勇敢饭饭,不怕刷题之链表】有序链表的合并
- 阻塞 非阻塞 poll机制 异步
- 2023版揽胜运动曝光,安全、舒适一个不落
- mysql5.7 installation and deployment - yum installation
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- In August the DB list latest scores - database Engines
- 开发模式对测试的影响
- 杭电多校-Loop-(不确定性贪心+线段树)
猜你喜欢
From the product dimension, why can't we fully trust Layer2?
首次入选OSDI顶会!腾讯提出超大规模推荐系统的模型低延时更新方案
GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
HCIP ---- VLAN
关于“码农”的一点自嘲解构
Redis6 (1) - Introduction to NoSQL Database and Installation of Redis
Weilai-software development engineer side record
Mobile and PC compatible loading and toast message plugins
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
【Azure云】服务端点和私有链接有什么区别?观点(1)
随机推荐
Introduction to cross-end development of Taro applet
Mount [shell][mount -o loop]
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
Mobile and PC compatible loading and toast message plugins
Weilai-software development engineer side record
blocking non-blocking poll mechanism asynchronous
getParameter()与 getAttribute()的用法与区别
bus event bus use
谷歌数据中心发生“电力事故”造成 3 人受伤
OneFlow source code parsing: operator instructions executed in a virtual machine
Three-phase 380V rectified voltage
Redis6 (1) - Introduction to NoSQL Database and Installation of Redis
Several small projects that I have open sourced over the years
组合模式:Swift 实现
owl.carousel海报卡片Slider轮播切换
Redis(三)——配置文件详解、发布和订阅、新数据类型
三相380V整流后的电压
【Azure云】服务端点和私有链接有什么区别?观点(1)
Cybersecurity Notes 5 - Digital Signatures
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?