当前位置:网站首页>Interview question 01.06. string compression
Interview question 01.06. string compression
2022-07-28 01:41:00 【Welcome to the confrontation Road】
Subject requirements
String compression . The number of times characters are used to repeat , Write a method , Realize basic string compression function . such as , character string aabcccccaaa Will turn into a2b1c5a3. if “ Compress ” The string after does not get shorter , Returns the original string . You can assume that the string contains only uppercase and lowercase letters (a to z).
Example 1:
Input :"aabcccccaaa"
Output :"a2b1c5a3"
Example 2:
Input :"abbccd"
Output :"abbccd"
explain :"abbccd" Compressed as "a1b2c2d1", Longer than the original string length .
Tips :
The string length is [0, 50000] Within the scope of
class Solution {
public:
string compressString(string S) {
int len = S.size();
string s1;
int count = 0;
for(int i = 0; i < len; i++)// Traversal string
{
count++;// Number of times used to record letters
if(S[i] != S[i+1])// If the current letter is different from the next letter , Then store the letter and the number of times of the letter in s1 in
{
s1+=S[i];
s1 += to_string(count);
count = 0;
}
}
if(len > s1.size())
return s1;
else
return S;
}
};
Experience
to_string(2), Convert numbers to character form
边栏推荐
猜你喜欢

"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 supplementary problem solution (a, C, J)

Leetcode 2351. the first letter that appears twice

Qt 绘制系统简介

Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used

画刷和画笔

牛客网刷题训练(三)

华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘

MATLAB 44种动漫渐变色绘图程序

26.抽象化和模板思想

Unknown database ‘xxxxx‘
随机推荐
Lecture 16 of project practice: using the open close principle to realize the commodity price rule engine
Opengauss active / standby architecture works with keeplive
Standing at the crossroads of digital retail transformation, we need to look at it from a new perspective
C语言·指针
面试题 01.09. 字符串轮转
Day 013 一维数组练习
idea常用的快捷键汇总
Cesium add inundation analysis measurement area
Thoroughly understand kubernetes scheduling framework and plug-ins
BigDecimal common API
面试题 01.07. 旋转矩阵
登录功能实现
3000 words and 11 pictures hard core popular science: what is edge computing? What are the connections and differences with cloud computing?
Cap principle of [distributed development]
软件测试面试题:性能测试指标有哪些?
Oracle Rac 集群文件目录迁移
20 bad habits of bad programmers
Qlib教程——基于源码(二)本地数据保存与加载
【样式集合1】tab 栏
自定义事件