【leetcode】两句话中的不常见单词

 

char ** uncommonFromSentences(char * A, char * B, int* returnSize){
    int alen = strlen(A);
    int blen = strlen(B);

    if (!alen && !blen)
    {
        * returnSize = 0;
        return NULL;
    }
    char* str = (char*)calloc(alen+blen+2,1); 
    if (alen)
    {
        strcpy(str,A);
        strcat(str," ");
        strcat(str,B);
    }
    else strcat(str, B);
    int len = strlen(str);
    char** arr = (char**)malloc(201*sizeof(char*));
    char* s[200] = {0};
    int hash[200] = {0};
    int index = 0;
    int flag = true;
    char* p = str;
    int n = 0;
    for (int i=0; i<len+1; i++)
    {
        if (str[i] == ' ' || str[i] == '\0')
        {
            str[i] = '\0';
            for(int j=0; j<=index; j++)
            {            
                if (s[j] && !strcmp(s[j],p))
                {
                    hash[j]++;
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                hash[index]++;
                s[index++] = p;
            }
            flag = true;
            p = &str[i+1];
        }
    }
    for (int j=0; j<index; j++)
    {
        if (hash[j]<2)
        {
            arr[n++] =  s[j];
        }
    }
    *returnSize = n;
    return arr;
}

 

上一篇:C++项目通过JNI使用Java第三方jar包


下一篇:关于vs strcpy_s()strcat_s()用法