My accepted c code (0ms)


  • 0
    R
    int comp_str(char** str1,char** str2){
        return strcmp(*str1,*str2);
    }
    
    char* longestCommonPrefix(char** strs, int strsSize) {
        if(strsSize==0) return "";
        if(strsSize==1) return *strs;
        
        qsort(strs,strsSize,sizeof(char*),comp_str);
        
        char* firstStr=*strs;
        char* lastStr=strs[strsSize-1];
        
        //if(strcmp(firstStr,"")==0) return "";
        
        int n=0;
        //for(;firstStr[n];n++);
        //char* prefix=malloc(sizeof(char)*(n+1));
        // n=0;
        while(firstStr[n]){
            if(firstStr[n]!=lastStr[n]) break;
            n++;
            
            
        }
        if(n>0) firstStr[n]='\0';
        else firstStr="";
        return firstStr;
    }

Log in to reply
 

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