当前位置:网站首页>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;
}
};
边栏推荐
- retired paddling
- URL.createObjectURL、URL.revokeObjectURL、Uint8Array、Blob使用详解
- Solve vscode input! Unable to quickly generate skeletons (three methods for the new version of vscode to quickly generate skeletons)
- shell--面试题
- Guangyu Mingdao was selected into the list of pilot demonstration projects for the development of digital economy industry in Chongqing in 2022
- ClickHouse多种安装方式
- 跨域网络资源文件下载
- STM32 Personal Notes - Watchdog
- pve 删除虚拟机「建议收藏」
- C#/VB.NET 将PPT或PPTX转换为图像
猜你喜欢
Endorsed in 2022 years inventory | product base, science and technology, guangzhou automobile group striding forward
解决vscode输入! 无法快捷生成骨架(新版vscode快速生成骨架的三种方法)
Visualization - Superset installation and deployment
Stone Technology builds hard-core brand power and continues to expand the global market
WPF 截图控件之绘制箭头(五)「仿微信」
xss-labs靶场挑战
Why Metropolis–Hastings Works
图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
Promise learning (1) What is Promise?how to use?How to solve callback hell?
CTFshow,命令执行:web33
随机推荐
Mysql index related knowledge review one
浏览器快捷键大全
[Cloud Residency Co-Creation] Huawei Cloud Global Scheduling Technology and Practice of Distributed Technology
cisco交换机基本配置命令(华为交换机保存命令是什么)
Browser shortcut keys
STM32 Personal Notes - Watchdog
activiti工作流的分页查询避坑
报告:想学AI的学生数量已涨200%,老师都不够用了
机器学习 | MATLAB实现支持向量机回归RegressionSVM参数设定
Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (4) Opening Report
力扣解法汇总1374-生成每种字符都是奇数个的字符串
从零开始Blazor Server(4)--登录系统
【likeshop】回收租凭系统100%开源无加密 商城+回收+租赁
如何设计一个分布式 ID 发号器?
冰冰学习笔记:gcc、gdb等工具的使用
Pve delete virtual machine "for a collection"
RK3399 platform development series on introduction to (kernel) 1.52, printk function analysis - the function call will be closed
Py之yellowbrick:yellowbrick的简介、安装、使用方法之详细攻略
pgAdmin 4 v6.12 发布,PostgreSQL 开源图形化管理工具
CTFshow,命令执行:web34、35、36