-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
95 lines (82 loc) · 2.39 KB
/
res.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
86
87
88
89
90
91
92
93
94
95
/**
* Given a string, find the length of the longest substring without repeating characters.
*
* Examples:
*
* Given "abcabcbb", the answer is "abc", which the length is 3.
*
* Given "bbbbb", the answer is "b", with the length of 1.
*
* Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*
* res.js
* @authors Joe Jiang (hijiangtao@gmail.com)
* @date 2017-02-27 20:52:27
* @version $Id$
*
* @param {string} s
* @return {number}
*/
let lengthOfLongestSubstring = function(s) {
let strlen = s.length;
// 字符串长度小于2
if (strlen < 2) {
return strlen;
}
let maxString = '', maxLen = 1;
for (let i=0; i<strlen; i++) {
let substr = [s[i]], continueJud = false;
// 剩余字符串即使全部不重复也不可能长于现有最长的字符串
if ( strlen-i<=maxLen ) {
break;
}
// 如果遇到连续相等的字符,则跳到本次连续出现的最后一个该字符上
while (i+1<strlen && s[i]===s[i+1]) {
i += 1;
continueJud = true;
}
if (continueJud) {
continueJud = false;
i -= 1;
continue;
}
// 从第二个字符开始遍历,直到字符串结尾或者出现重复字符的情况
for (let j=i+1; j<strlen; j++) {
if (substr.indexOf(s[j]) !== -1) {
if (substr.length > maxLen) {
maxLen = substr.length;
// maxString = ''.join(substr);
}
break;
}
substr.push(s[j]);
if (j === strlen-1) {
if (substr.length > maxLen) {
maxLen = substr.length;
}
}
}
}
return maxLen;
};
/**
* 滑动窗口
* @param {*} s
*/
let lengthOfLongestSubstring_2 = function(s) {
let strlen = s.length;
let start = 0;
let strdict = {};
let max = 0;
for (let i = 0; i < strlen; i++) {
const e = s[i];
if (strdict[e] !== undefined && strdict[e] >= start) {
start = strdict[e] + 1;
} else {
const curLen = i-start+1;
max = Math.max(curLen, max);
}
strdict[e] = i;
}
return max;
}