Skip to content

Commit ea640ea

Browse files
committed
lengthOfLongestSubstring
1 parent 882e6e2 commit ea640ea

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lengthOfLongestSubstring.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @Author: Z1hgq
3+
* @Date: 2019-09-17 14:26:50
4+
* @LastEditTime: 2019-09-17 18:05:19
5+
*/
6+
7+
/**
8+
* @param {string} s
9+
* @return {number}
10+
*/
11+
12+
var lengthOfLongestSubstring = function(s) {
13+
var res = 0,
14+
i = 0;
15+
var temp = [];
16+
while(i < s.length) {
17+
if(temp.indexOf(s[i]) === -1) { //如果不存在的话向数组里继续添加
18+
temp.push(s[i]);
19+
} else {
20+
temp.shift(); //如果存在的话表明当前这个子串不符合无重复字符的要求,删除队头的元素,重新执行这次循环
21+
continue;
22+
}
23+
res = Math.max(res, temp.length);
24+
i++;
25+
}
26+
return res;
27+
};
28+
29+
//超时
30+
var lengthOfLongestSubstringII = function(s) {
31+
if(s === "") return 0;
32+
for(let i = s.length; i > 0; i--){
33+
let j = 0;
34+
while(j + i <= s.length){
35+
let str = s.slice(j, j + i);
36+
if(s.indexOf(str) != -1){
37+
let strSet = new Set(str);
38+
if(Array.from(strSet).length === str.length){
39+
return str.length;
40+
}else{
41+
j++;
42+
}
43+
}else{
44+
j++;
45+
}
46+
}
47+
}
48+
};
49+
console.log(lengthOfLongestSubstring("dvuadbiabkhbcacbnaosbduioheqwoehoiqwjojojaddddddddddddddohwdoboubqibibiuabisbdoahdiqowhdpqwpojlcnklasnclknalkdf"));

0 commit comments

Comments
 (0)