#include <algorithm>
class Solution {
public:
int compress(vector<char>& chars) {
int m = chars.size();
int i = 0, j = 1, tmp= 0;
int length;
while (i < m && j < m){
while (j < m && chars[j] == chars[i]) {
j++;
}
tmp = j;
length = j - i;
while (--j > i){
if (length != 0){
chars[j] = length % 10 + '0';
length = length / 10;
continue;
}
chars[j] = '"';
}
i = tmp;
j = tmp+1;
}
chars.erase(remove(chars.begin(), chars.end(), '"'), chars.end());
return chars.size();
}
};
压缩字符串-数组443-c++
![](https://img-home.csdnimg.cn/images/20240711042549.png)