当前位置:网站首页>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 <= 2000chars[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;
}
}
边栏推荐
- The impact of development mode on testing
- bus事件总线 使用
- 1-IMU参数解析以及选择
- Several small projects that I have open sourced over the years
- Redis(三)——配置文件详解、发布和订阅、新数据类型
- 3 injured in 'electrical accident' at Google data center
- Unsafe的一些使用技巧
- 短视频软件开发——平台同质化如何破局
- Cybersecurity Notes 5 - Digital Signatures
- Weilai-software development engineer side record
猜你喜欢

Memory problems difficult to locate, it is because you do not use ASAN

3 injured in 'electrical accident' at Google data center

4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?

What is an abstract class

Redis6 (1) - Introduction to NoSQL Database and Installation of Redis

In August the DB list latest scores - database Engines

"MySQL Advanced Chapter" 6. Principles of index creation and design

8月份DB-Engines 数据库排行榜最新战况

Dry goods!ASSANet: Making PointNet++ faster and stronger
![[Microservice Architecture] Microservices and SOA Architecture (2)](/img/62/70e5cd1640bc7a78525cdb662adffe.png)
[Microservice Architecture] Microservices and SOA Architecture (2)
随机推荐
首次入选OSDI顶会!腾讯提出超大规模推荐系统的模型低延时更新方案
使用cpolar远程连接群晖NAS(升级固定链接2)
技能大赛训练题:组策略一
第2章-矩阵及其运算-矩阵创建(1)
程序员追求技术夯实基础学习路线建议
干货!ASSANet:让PointNet++更快更强
SQL与NoSQL最终会走向融合吗?
bus事件总线 使用
Several small projects that I have open sourced over the years
owl.carousel poster card Slider carousel switch
Unsafe的一些使用技巧
从产品角度看 L2 应用:为什么说这是一个游乐场?
一文带你搞懂中断按键驱动程序之poll机制
网络安全笔记5——数字签名
什么是抽象类
PPT | 「课件」企业中高层人员安全管理培训(118页)
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
【电商运营】你真的了解社交媒体营销(SMM)吗?
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
蔚来-软件开发工程师一面记录