-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path003.无重复字符的最长子串.js
85 lines (73 loc) · 2.09 KB
/
003.无重复字符的最长子串.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Created by Administrator on 2018/4/20.
*/
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
if (!s) return 0;
const map = {};
let max = 1;
for (let i = 0, j = 0; j < s.length; j++) {
if (s.indexOf(s[j]) !== j) {
// 能进入到这里说明s[j]已经不是第一次遍历到了
// 所以i直接取上次记录的最远位置+1就可以了
i = i <= map[s[j]] ? map[s[j]] + 1 : i;
}
// 更新全局最大值
max = Math.max(max, j - i + 1);
// 记录每次遍历的时候元素在这个里面的最远位置
map[s[j]] = j;
}
return max;
};
/**双指针(滑动窗口的最大值)
* 指针i j,置于字符串的开始位置
* j先移动,如果碰到重复的了,记录此时的最大值,i移动
* 循环往复
* 如果i到到末尾了,返回全局最大
*/
var lengthOfLongestSubstring = function (s) {
let res = 0;
let len = s.length;
if (len < 2) return len;
let i = 0;
let j = 0;
let map = {};
while (j < len) {
let end = s[j];
if (map[end] === undefined) {
map[end] = j;
} else {
let max = j - i;
res = Math.max(max, res);
// 其实我们已经知道目标位置了,因为先前存了这个数的位置
// 这里只需要讲前面的窗口恢复即可
while (i <= map[end]) {
map[s[i]] = undefined;
i++;
}
map[end] = j;
}
j++;
// 最后一次结束的时候看一下最大值
if (j === len) {
res = Math.max(j - i, res);
}
}
return res;
};
let test = [
['aab', 2],
['abcabcbb', 3],
['bbbbb', 1],
['pwwkew', 3],
['abcbadbb123', 4],
['wcibxubumenmeyatdrmydiajxloghiqfmzhlvihjouvsuyoypayulyeimuotehzriicfskpggkbbipzzrzucxamludfyk', 12],
];
test.forEach(t => {
if (lengthOfLongestSubstring(t[0]) !== t[1]) {
console.log(t[0], lengthOfLongestSubstring(t[0]));
}
})