当前位置:网站首页>leetcode每日一题:字符串压缩
leetcode每日一题:字符串压缩
2022-08-01 10:51:00 【利刃Cc】
面试题 01.06. 字符串压缩
难度简单142
字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母(a至z)。
示例1:
输入:"aabcccccaaa"
输出:"a2b1c5a3"
示例2:
输入:"abbccd"
输出:"abbccd"
解释:"abbccd"压缩后为"a1b2c2d1",比原字符串长度更长。
提示:
- 字符串长度在[0, 50000]范围内。
思路一:
- 用 tmp 来存储新的字符串,用 count 记录重复出现的字符。
- 一开始对字符串 s 进行遍历,然后将每次第一次出现的字符赋给 tmp,然后判断一下字符串 s[i] == s[i + 1],若相等则让 count++ ,不相等则将 count 运用一个函数 to_string 转化为字符串然后尾插到 tmp,
- 然后继续循环,直到遍历结束。
class Solution {
public:
string compressString(string S) {
string tmp;
int count = 1;
for(int i = 0; i < S.size(); ++i)
{
//只将第一次出现的那个附上去,其他的pop掉
if(count == 1)
tmp += S[i];
//若前后两个相同则直接count++
if(S[i] == S[i+1])
{
count++;
}
//前后两个不同则将出现的次数尾插到tmp处
else
{
tmp += to_string(count);//使用to_string函数转化为sting类型
count = 1;
}
}
//将长度短的string返回
return tmp.size() < S.size() ? tmp : S;
}
};
思路二:
- 运用双指针,让 i 指向第一次出现的字符,然后 在内层循环让 j 去遍历找到和 i 处不同的字符
- 若找到了不同的则与思路1一样,将 i 处的字符尾插到 tmp,然后将长度 j - i 转化为字符串尾插到 tmp 后面
- 然后将 i 移到 j 处,继续循环,直到遍历结束。
class Solution {
public:
string compressString(string S) {
int i = 0, j = 0;
int n = S.size();
string tmp;
// 「外层循环」i 指向每个首次出现的字符
while (i < n)
{
// 「内层循环」j 向前遍历,直到字符串末尾或找到与 s[i] 不同的字符时跳出
while (j < n && S[i] == S[j])
j++;
// 压缩字符串,添加至 tmp
tmp += S[i];
tmp += to_string(j - i);
// 令 i 指向下一个首次出现的字符
i = j;
}
// 对比「压缩字符串」和「原字符串」长度,返回较短的
return tmp.size() < n ? tmp : S;
}
};
边栏推荐
- Promise学习(二)一篇文章带你快速了解Promise中的常用API
- 利用正则表达式的回溯实现绕过
- PDMan-domestic free general database modeling tool (minimalist, beautiful)
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(4)开题报告
- The first experience of Shengsi large model experience platform——Take the small model LeNet as an example
- EasyRecovery热门免费数据检测修复软件
- For small applications, which database is better to use?
- 进制与转换、关键字
- .NET性能优化-使用SourceGenerator-Logger记录日志
- STM32 Personal Notes - Watchdog
猜你喜欢

CTFshow,命令执行:web31

如何从完美的智能合约中窃取 1 亿美元

一文说明白ECDSA spec256k1 spec256r1 EdDSA ed25519千丝万缕的关系

利用正则表达式的回溯实现绕过

已解决(pip安装库报错)Consider using the-- user option or check the permissions.

.NET深入解析LINQ框架(三:LINQ优雅的前奏)

Promise learning (2) An article takes you to quickly understand the common APIs in Promise

ClickHouse入门介绍与其特性

xss-labs靶场挑战
retired paddling
随机推荐
redis6 跟着b站尚硅谷学习
gc的意义和触发条件
CTO strongly banning the use of the Calendar, that in what?
世界第4疯狂的科学家,在103岁生日那天去世了
解决vscode输入! 无法快捷生成骨架(新版vscode快速生成骨架的三种方法)
Introduction to data warehouse layering (real-time data warehouse architecture)
Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (2) Mini Program Functions
.NET性能优化-使用SourceGenerator-Logger记录日志
线上问题排查常用命令,总结太全了,建议收藏!!
MySQL 必现之死锁
数仓分层简介(实时数仓架构)
这是我见过写得最烂的Controller层代码,没有之一!
How to find out hidden computer software (how to clean up the computer software hidden)
Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (4) Opening Report
正则表达式
Cross-domain network resource file download
CTFshow,命令执行:web33
Dataset之mpg:mpg数据集的简介、下载、使用方法之详细攻略
昇思大模型体验平台初体验——以小模型LeNet为例
一篇文章,带你详细了解华为认证体系证书(2)