Why runtime exception? last input is ["a"], language is C.


  • 0
    R

    this is my program:

    char *longestCommonPrefix(char *strs[], int n) {
    if(n<=0||strs[0]==NULL) return NULL;

    int pos=strlen(strs[0]);
    char* result=strs[0];
    int i;
    
    for(i=1;i<n;i++){
        pos=pos>strlen(strs[i])?strlen(strs[i]):pos;
        
        while(strncmp(result,strs[i],pos)){
            pos--;
            if(pos==0) return "";
        }
    }
    
    result[pos]='\0';
    return result;
    

    }


Log in to reply
 

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