A simple C solution[Accepted]


  • 0
    B
    char* longestCommonPrefix(char** strs, int strsSize) {
        if(strsSize==0){return "";}
        if(strsSize==1){return strs[0];}
        for(int i=1;i<strsSize;i++){
            for(int j=0;strs[0][j]!='\0';j++){
                if(strs[i][j]!=strs[0][j]){
                    strs[0][j]='\0';
                    if(j==0){return "";}
                    break;
                }
            }
        }
        return strs[0];
    }
    

Log in to reply
 

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