1ms java solution


  • 0
    H
    public String longestCommonPrefix(String[] strs) {
        
        int arrayLen = strs.length;
        if (strs == null || strs.length == 0) return "";
        if (strs.length == 1) return strs[0];
        
        int minlen = strs[0].length();
        int len;
        for (int i = 1; i < arrayLen; i++) {
            len = strs[i].length();
            if (minlen > len) minlen = len;
        } // for
        
        char model;
        int i,j;
        for (i = 0; i < minlen; i++) {
            model = strs[0].charAt(i);
            for (j = 0; j < arrayLen; j++) {
                if (strs[j].charAt(i)!=model) break;
            }
            if (j!=arrayLen) break;
        }
        return strs[0].substring(0,i);
    }

Log in to reply
 

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