Skip to content

Commit f5f2cdc

Browse files
committed
Add solution of: 003 Longest Substring Without Repeating Characters
1 parent 40d486d commit f5f2cdc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LeetCode
55
|----|--------|---------|-------|
66
|1|[Two Sum][1]|[JavaScript](./solution/001.Two_Sum/Solution.js)|Easy|
77
|2|[Add Tow Numbers][2]|[JavaScript](./solution/002.Add_Two_Numbers/Solution.js)|Medium|
8+
|3|[Longest Substring Without Repeating Characters][3]|[JavaScript](./solution/003.Longest_Substring_Without_Repeating_Characters/Solution.js)|Medium|
89

910
[1]:https://leetcode.com/problems/two-sum/
1011
[2]:https://leetcode.com/problems/add-two-numbers/
12+
[3]:https://leetcode.com/problems/longest-substring-without-repeating-characters/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
var maxLength = 0;
7+
for (var i = 0; i < s.length; i++) {
8+
if (s.length - i <= maxLength) break;
9+
var map = {};
10+
var length = 0;
11+
for (var j = i; j < s.length; j++) {
12+
var charCode = s.charCodeAt(j);
13+
if (map[charCode] !== null && map[charCode] !== undefined) {
14+
break;
15+
}
16+
map[charCode] = j;
17+
length++;
18+
}
19+
if (length > maxLength) maxLength = length;
20+
// console.log('from index(' + i + ') maxLength = ' + length + ', substr = ' + s.substr(i, length));
21+
}
22+
return maxLength;
23+
};
24+
25+
// test
26+
function test() {
27+
var testString = "abcabcbb";
28+
console.log('string = ' + testString);
29+
console.log('index = ' + (function(s){
30+
var s = "";
31+
for (var i = 0; i < s.length; i++) {
32+
s += i;
33+
};
34+
return s;
35+
})(testString));
36+
console.log("length = " + lengthOfLongestSubstring(testString));
37+
}
38+
39+
test();

0 commit comments

Comments
 (0)