3ms C solution


  • 0
    D
    char* longestCommonPrefix(char** strs, int strsSize) {
        int min = INT_MAX;
        for(int i=0;i<strsSize;i++){
            if (min>strlen(strs[i])) min=strlen(strs[i]);
        }
        if(strsSize==0) return "";
        char* re = (char*)malloc(sizeof(char)*200);
        memset(re,0,sizeof(char)*200);
        char temp;
        for(int i=0;i<min;i++){
            temp = strs[0][i];
            for(int j=1;j<strsSize;j++){
                if (temp!=strs[j][i]) return re;
            }
            re[i] = temp;
        }
        return re;
    }
    

Log in to reply
 

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