思路:就是在for循环中遍历字符串,如果有重复的字符则进入while循环,同时int temp计数,最后加上。
class Zipper {
public:
string zipString(string iniString)
{
string ret;
for(int i=0;i<=iniString.length();++i)
{
ret+=iniString[i];
int temp=1;
if(i+1<=iniString.length() && iniString[i+1]==iniString[i])
{
while(i+1<=iniString.length() && iniString[i+1]==iniString[i])
{
++temp;
++i;
}
}
ret+=to_string(temp);
}
if(iniString.size()<=ret.size())
{
return iniString;
}
return ret;
}
};