题解 | #压缩字符串(一)#
压缩字符串(一)
https://www.nowcoder.com/practice/c43a0d72d29941c1b65c857d8ac9047e
#include <string> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param param string字符串 * @return string字符串 */ string compressString(string param) { // write code here if(param.empty()){ return param; } string str = param; // string str_tmp = str; string str_tmp; int cnt = 1; for (int i = 0; i <= str.size() - 1; i++) { if (str[i + 1] == str[i]) { cnt++; } else { if (cnt != 1) { str_tmp += str[i]; string str_num = std::to_string(cnt); str_tmp += str_num; cnt = 1; }else{ str_tmp+=str[i]; cnt=1; } } } return str_tmp; } };