-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.js
39 lines (37 loc) · 906 Bytes
/
Solution1.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
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
var maxLength = 0;
for (var i = 0; i < s.length; i++) {
if (s.length - i <= maxLength) break;
var map = {};
var length = 0;
for (var j = i; j < s.length; j++) {
var charCode = s.charCodeAt(j);
if (map[charCode] !== null && map[charCode] !== undefined) {
break;
}
map[charCode] = j;
length++;
}
if (length > maxLength) maxLength = length;
// console.log('from index(' + i + ') maxLength = ' + length + ', substr = ' + s.substr(i, length));
}
return maxLength;
};
// test
function test() {
var testString = "abcabcbb";
console.log('string = ' + testString);
console.log('index = ' + (function(s){
var s = "";
for (var i = 0; i < s.length; i++) {
s += i;
};
return s;
})(testString));
console.log("length = " + lengthOfLongestSubstring(testString));
}
test();