#include <iostream>
using namespace std;
#include <string>
class Solution {
public:
string compressString(string S) {
char temp = S[0];
int count = 1;
string out;
for (int i = 0; i <= S.length(); ++i)
{
if (i == 0)
{
out += S[0];
continue;
}
if (S[i] == temp)
{
count++;
}
else
{
out += to_string(count);
count = 1;
temp = S[i];
out += temp;
}
}
if (out.length() <= S.length())
{
return out;
}
else
{
return S;
}
}
};
int main()
{
Solution method;
string str = "abbccd";
string out = method.compressString(str);
cout << out << endl;
system("pause");
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-46367.html