#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;
}

上一篇:SSM框架MavenWeb项目的测试


下一篇:极简 Node.js 入门 - 5.1 创建 HTTP 服务器