C : Runtime Error (Last executed input: [])


  • 0
    S

    Hello guys,
    As title, please tell me why my code is wrong

    char* longestCommonPrefix(char** strs, int strsSize) 
    {
        char *ans;
        bool end = false;
        int off = 0;
        int size = strlen(strs[0]);
        
        if(strsSize < 1) return "";
        if(size == 0) return "";
        
        ans = malloc(size+1);
        memset(ans, 0, size);
        
        for(off = 0; off < size; ++off){
            for(int i = 1; i < strsSize; ++i){
                if(strs[0][off] != strs[i][off]){
                    end = true;
                    break;
                }
            }
            if(end){
                break;
            }
            ans[off] = strs[0][off];
        }
        
        ans[off] = '\0';
    
        return ans; 
    }
    

    Thanks!


  • 0
    S

    Solved by myself
    if strs[0] == NULL, do strlen will crush!


Log in to reply
 

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