【leetcode】压缩字符串

 

int compress(char* chars, int charsSize){
    int pst = 0;
    int anchor = 0;
    for (int i=0; i<charsSize; i++)
    {
        if (i == charsSize-1 || chars[i] != chars[i+1])
        {
            chars[pst++] = chars[i];
            if (i > anchor) 
            {
                char* buff = (char*)calloc(5,sizeof(int));
                sprintf(buff,"%d",i-anchor+1);
                for (int j=0; j<strlen(buff); j++)
                {
                    chars[pst++] = buff[j];
                }
            }
            anchor = i+1;
        }
    }
    return pst;
}

 

posted @ 2020-09-04 18:54  温暖了寂寞  阅读(153)  评论(0编辑  收藏  举报