题解 | #CM5 基本字符串压缩#
基本字符串压缩
http://www.nowcoder.com/practice/21f3a84300c94db092e0b5a7bf2d0ad1
class Zipper {
public:
string zipString(string iniString) {
// write code here
int i = 0,pos;
string ans = "";
while(i < iniString.size()){
ans += iniString[i];
pos = iniString.find_first_not_of(iniString[i],i);
if(pos < 0)
ans += to_string(iniString.size() - i);
else
ans += to_string(pos - i);
i = pos;
}
return ans.size() > iniString.size() ? iniString : ans;
}
};