My c 0ms solution


  • 1
    Y
    char* longestCommonPrefix(char** strs, int strsSize) {
        if (strsSize <= 0 || strs == NULL) return strs;
        if (strsSize == 1) return strs[0];
        for (int i = 1; i < strsSize; ++i) {
            char* s0 = strs[0];
            char* si = strs[i];
            int pos = 0;
            while (true) {
                if (s0[pos] != si[pos] || s0[pos] == NULL || si[pos] == NULL) {
                    s0[pos] = '\0';
                    if (pos <= 0) return s0;
                    break;
                }
                pos++;
            }
        }
        return strs[0];
    }

Log in to reply
 

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