Re: [闲聊] 每日leetcode

楼主: sixB (6B)   2024-11-04 08:16:49
3163.
超简单
不知道为啥5个月前的我会MLE
完全照着题目要求跑就好
一个字一个字读
class Solution {
public:
string compressedString(string word) {
if(word.length() == 0) return "";
string res = "";
char c = word[0];
int cnt = 0;
int len = word.length();
for(int i = 0; i < len ; i++){
if(word[i] != c){
if(cnt > 0){
res += ('0' + cnt);
res += c;
}
c = word[i];
cnt = 1;
}
else{
cnt++;
}
if(cnt == 9){
res += '9';
res += c;
cnt = 0;
}
}
if(cnt > 0){
res += ('0' + cnt);
res += word[len-1];
}
return res;
}
};
作者: sustainer123 (caster)   2024-11-04 08:17:00
大师
作者: Furina (芙宁娜)   2024-11-04 08:42:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com