arshelia / algorithm-js

LeetCode 刷题
0 stars 0 forks source link

14.最长公共前缀 #10

Open arshelia opened 4 years ago

arshelia commented 4 years ago

题目

链接: https://leetcode-cn.com/problems/longest-common-prefix/

arshelia commented 4 years ago
var longestCommonPrefix = function(strs) {
    if(!strs.length) return "";
    var str = strs[0];
    for(let i = 0; i < str.length; i++){
        var temp = str.charAt(i);
        for(let j = 1; j < strs.length; j++){
            if(i > strs[j].length || strs[j].charAt(i) !== temp){
                return str.substr(0, i);
            }
        }
    }
    return str;
};