As a c coder,I can only do it in a not elegant way~


  • 0
    S
    char** findRestaurant(char** list1, int list1Size, char** list2, int list2Size, int* returnSize) {
        int i=0,j=0,min=2001,count=0;
        char buf[1000][31]={0X0};
        for(i=0;i<list1Size;i++){
            for(j=0;j<list2Size;j++){
                if(strcmp(list1[i],list2[j])==0){
                    if((i+j) < min)  {
                        printf("min=%d,i=%d,j=%d\n",min,i,j);
                        min=(i+j);
                        memset(buf,0,sizeof(buf));
                        count=0;
                        strcpy(buf[count++],list1[i]);
                        puts(list1[i]);
                    }
                    else if((i+j) == min){
                        strcpy(buf[count++],list1[i]);
                        puts(list1[i]);
                    }
                }
            }
        }
        
        *returnSize = count;
        char** retarr=(char**)calloc(*returnSize ,sizeof(char*));
        for(i=0;i<count;i++){
            retarr[i]=calloc(31,sizeof(char));
            strcpy(retarr[i],buf[i]);
        }
        return retarr;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.