其他分享
首页 > 其他分享> > #763 划分字母区间

#763 划分字母区间

作者:互联网

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* partitionLabels(char * s, int* returnSize){
    int *result = (int *)malloc(sizeof(int) * strlen(s));
    *returnSize = 0;
    int i;
    int rightIndex = -1;
    for(i = 0; i<strlen(s); ++i)
    {
        int j;
        for(j = strlen(s)-1; j>i; j--)
        {
            if(s[j] == s[i]) break;
        }
        if(j > rightIndex) rightIndex = j;
        if(i == rightIndex)
        {
            if((*returnSize) == 0) result[(*returnSize)] = rightIndex + 1;
            else 
            {
                int k;
                for(k=0; k<(*returnSize); ++k)
                {
                    rightIndex -= result[k];
                }
                result[(*returnSize)] = rightIndex + 1;
            }
            (*returnSize)++;
            rightIndex = -1;
        }
    }
    return result;
}

标签:malloc,returnSize,int,763,字母,划分,result,rightIndex
来源: https://blog.csdn.net/Haku_yyf/article/details/120255919