题解 | #压缩字符串(一)#

压缩字符串(一)

https://www.nowcoder.com/practice/c43a0d72d29941c1b65c857d8ac9047e

#include <string>
class Solution {
public:

    string compressString(string param) {
        int n = param.size();
        string ret;
        int count=0,left=0,right=0;
        while(right<n)
        {
            ret += param[left];
            while(right<n && param[left]==param[right])
            {
                count++;
                right++;
            }
            if(count>1)
            {
               
                ret += to_string(count);
            }
            count = 0;
            left = right;
        }
        return ret;
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务