forked from HCThink/h-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lee14.js
47 lines (43 loc) · 1.11 KB
/
lee14.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 最长公共前缀
* [
* 'a b c'
* 'a b d'
* 'a b f'
* 'a b c d'
* ]
*
* 思路: 将这个字符串数组看做二维数组.我们外层无限循环, 因为不知道最短的字符串
*/
function longestCommonPrefix(strs = []) {
let i = 0
if (strs.length < 2) {
return strs[0] || ''
}
while(true) {
for (let j = 1; j < strs.length; j++) {
debugger;
if (strs[0][i] !== strs[j][i] || strs[0].length === i || strs[j].length === i) {
return strs[0].substring(0, i);
}
}
i++;
}
return strs[0].substring(0, i);
}
console.log(longestCommonPrefix(['abdd', 'abdww', 'abdcdd']))
/**
* leecode 耗时最少解法
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {
if (strs.length == 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++)
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix.length == 0) return "";
}
return prefix;
};