Skip to content

Latest commit

 

History

History
27 lines (27 loc) · 504 Bytes

M01.06-CompressStringLCCI.md

File metadata and controls

27 lines (27 loc) · 504 Bytes

思路

双指针

class Solution 
{
public:
    string compressString(string S) 
    {
        if (S.size() == 0) return "";
        int i = 0;
        int j = 0;
        string ans = "";
        while (i < S.size())
        {
            j = i;
            while (j < S.size() && S[j] == S[i])
            {
                j++;
            }
            ans += S[i];
            ans += to_string(j-i);
            i = j;
        }
        return ans.size() >= S.size() ? S : ans;
    }
};