Skip to content

Commit 0280c02

Browse files
add: 增加一个算法题
1 parent 5a006d4 commit 0280c02

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/string/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
总结:
2+
1、遇到那种重复的基本上都可以用Map
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
*/

0 commit comments

Comments
 (0)