What's wrong with my c code? My code works fine in my mac's xcode.


  • 3
    D

    Here is my code:

    Submission Result: Runtime Error
    Last executed input: ["",""]

    char *longestCommonPrefix(char *strs[], int n) {
        if (n == 0) return NULL;
        int length = 0;
        char *p = strs[0];
        int found = 0;
        while (*strs[0] != '\0') {
            for (int i = 0; i < n - 1; i++) {
                if (*strs[i] != *strs[i + 1]) {
                    found = 1;
                    break;
                }
                strs[i]++;
            }
            if (found) break;
            strs[n - 1]++;
            length++;
        }
        char *ans = (char*)malloc(sizeof(char*)*(length + 1));
        strncpy(ans, p, length);
        ans[length] = '\0';
        return ans;
    }

  • 0
    B

    I don't think this is the reason, but " sizeof(char*) " is wrong.


  • 0
    P

    I got the same error: Runtime Error Last executed input: ["",""] and I think my code is working fine locally.


  • 0
    D

    i got the same problem...


  • 0
    D

    I met the same RE too. But it does run well in my computer.


Log in to reply
 

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