File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
solution/003.Longest_Substring_Without_Repeating_Characters Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 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/
Original file line number Diff line number Diff line change 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 ( ) ;
You can’t perform that action at this time.
0 commit comments