forked from future-builder007/algorithm_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05.js
More file actions
21 lines (20 loc) · 706 Bytes
/
Copy path05.js
File metadata and controls
21 lines (20 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 14. 最长公共前缀 难度:简单
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {
let one = strs.length > 0 ? String(strs[0]).split("") : false; // 拿出数组中其中一个就可以
let str = "";
if (!one) { return str; };
for (let i = 0; i < one.length; i++) {
let num = 0;
strs.map(item => { // 便利每个单词是否和第一个单词有最长公共前缀
item.charAt(i) == one[i] ? num++ : null
})
console.log(num)
if (num === strs.length) { str = str + one[i] } else { break }
}
return str;
};
console.log(longestCommonPrefix(["flower", "flow", "flight"]))