File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ 总结:
2
+ 1、遇到那种重复的基本上都可以用Map
Original file line number Diff line number Diff line change
1
+ function lengthOfLongsSubstring ( s ) {
2
+ let myMap = new Map ( )
3
+ let l = 0 , r = 0 , maxL = 0
4
+ while ( r < s . length ) {
5
+ if ( myMap . has ( s [ r ] ) ) {
6
+ l = l > myMap . get ( s [ r ] ) + 1 ? l : myMap . get ( s [ r ] ) + 1
7
+ }
8
+ myMap . set ( s [ r ] , r )
9
+ maxL = Math . max ( maxL , r - l + 1 )
10
+ r ++
11
+ }
12
+
13
+ return maxL
14
+ }
15
+
16
+ console . log ( lengthOfLongsSubstring ( 'abcabcbb' ) ) // 'abc' 3
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
+ let map = new Map()
43
+ let left = 0, right = 0, maxLen = 0
44
+ while(right < s.length) {
45
+ let w = s[right]
46
+ if(map.has(w)) {
47
+ let i = map.get(w) + 1
48
+ left = left > i ? left : i
49
+ }
50
+ map.set(w, right)
51
+ maxLen = Math.max(maxLen, right-left+1)
52
+ right++
53
+ }
54
+ return maxLen
55
+
56
+ */
You can’t perform that action at this time.
0 commit comments