题解 | #暴力查找--最长公共前缀#
最长公共前缀
http://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47
function longestCommonPrefix( strs ) {
// write code here
if(strs.length == 1) return strs[0];
if(strs.includes("") || strs.length==0) return ""; //含有“”、数组长度为0
let str = strs[0];
for(let i = 0;i<str.length;i++){
for(let j = 1;j<strs.length;j++){
if(str[i] != strs[j][i]){
return str.slice(0,i);
}
}
}
return str;
}
module.exports = {
longestCommonPrefix : longestCommonPrefix
};