题解 | #最长公共前缀# 纵向比较
最长公共前缀
http://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47
/**
*
* @param strs string字符串一维数组
* @return string字符串
*/
function longestCommonPrefix( strs ) {
// write code here
if(!strs.length) return ""
let res = ""
let falg = true
for(let i = 0; i<strs[0].length; i++) {
const str = strs[0][i]
for(let j =1; j<strs.length; j++) {
if(strs[j][i] != str) {
falg = false
break;
} else {
falg = true
}
}
if(falg) res += str
}
return res
}
module.exports = {
longestCommonPrefix : longestCommonPrefix
};