SOURCE

var longestCommonPrefix = function(strs) {
    if (strs.length == 0) {
        return "";
    }
    var minLen = strs[0].length;
    var publicStr = '';
    for (var i = 1; i < strs.length; i++) {
        minLen = strs[i].length < minLen ? strs[i].length : minLen;
    }
    for (var j = 0; j < minLen; j++) {
        var temp = strs[0][j];
        for (var k = 1; k < strs.length; k++) {
            if (strs[k][j] != temp) {
                temp = '';
                break;
            } 
        }
        if (temp == '') {
            break;
        } else {
            publicStr += temp;
        }
    }
    return publicStr;
};
var strs = ["flower","flow","flight"];
console.log(longestCommonPrefix(strs))
console 命令行工具 X clear

                    
>
console