Skip to content

Commit 497e249

Browse files
committed
feat(leetcode): p3
1 parent 15ada0d commit 497e249

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed

docs/.vuepress/utils/alias.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@
5555
"sword-means-offer-------": "",
5656
"sword-means-offer": "剑指 Offer",
5757
"03-find-repeat-number": "03. 数组中重复的数字",
58-
"64-sum-1-n-nums": "64 求1 ~ n的和"
59-
}
58+
"64-sum-1-n-nums": "64 求1 ~ n的和",
59+
"leetcode-------": "",
60+
"leetcode": "Leetcode",
61+
"p3": "无重复字符的最长子串"
62+
}

docs/zh/leetcode/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LeetCode
2+
3+
[03 - 无重复字符的最长子串](./p3/README.md)

docs/zh/leetcode/p3/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## 题目描述
2+
3+
:::tip
4+
给定一个字符串 s ,请你找出其中不含有重复字符的 **最长子串** 的长度。
5+
[leetcode problems](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
6+
:::
7+
8+
## 解题思路 or 实现原理
9+
10+
:::tip
11+
使用 set 存储数据。通过两个指针,i,j,当 !set.has(s[j]),maxLength 增加;否则,在判断set.has(s[i]),删除 set中的 s[j],指针向后偏移;将当前 s[i] 加入到 set 中
12+
:::
13+
14+
举个例子
15+
16+
第1步
17+
```
18+
i: 0
19+
j: 0
20+
maxLength: 0 -> 1
21+
set: [] -> [a]
22+
23+
i j
24+
|
25+
v
26+
a b a b
27+
```
28+
29+
第2步
30+
```
31+
i: 1
32+
j: 0
33+
maxLength: 1 -> 2
34+
set: [a] -> [a, b]
35+
i j
36+
|
37+
v
38+
a b a b
39+
40+
j i
41+
| |
42+
v v
43+
a b a b
44+
```
45+
46+
第3步
47+
```
48+
i: 2
49+
j: 1
50+
maxLength: 2
51+
set: [a, b] -> [a, b, a] -> [b, a]
52+
53+
j i
54+
| |
55+
v v
56+
a b a b
57+
58+
j i
59+
| |
60+
v v
61+
a b a b
62+
```
63+
64+
第4步
65+
```
66+
i: 3
67+
j: 2
68+
maxLength: 2
69+
set: [b, a] -> [b, a, b] -> [a, b]
70+
71+
j i
72+
| |
73+
v v
74+
a b a b
75+
76+
j i
77+
| |
78+
v v
79+
a b a b
80+
```
81+
82+
## 实现代码
83+
84+
<<< @/src/leetcode/leetcode-3/index.ts
85+
86+
## 参考

src/leetcode/leetcode-3/README.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## 题目描述
2+
3+
:::tip
4+
给定一个字符串 s ,请你找出其中不含有重复字符的 **最长子串** 的长度。
5+
[leetcode problems](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
6+
:::
7+
8+
## 解题思路 or 实现原理
9+
10+
使用 set 存储数据,保证唯一性。通过两个指针,i,j,当 !set.has(s[j]),maxLength 增加;否则,在判断set.has(s[i]),删除 set中的 s[j],指针向后偏移;将当前 s[i] 加入到 set 中
11+
12+
举个例子
13+
14+
第1步
15+
```
16+
maxLength: 0 -> 1
17+
set: [] -> [a]
18+
19+
i j
20+
|
21+
v
22+
a b a b
23+
```
24+
25+
第2步
26+
```
27+
maxLength: 1 -> 2
28+
set: [a] -> [a, b]
29+
i j
30+
|
31+
v
32+
a b a b
33+
34+
j i
35+
| |
36+
v v
37+
a b a b
38+
```
39+
40+
第3步
41+
```
42+
maxLength: 2
43+
set: [a, b] -> [a, b, a] -> [b, a]
44+
45+
j i
46+
| |
47+
v v
48+
a b a b
49+
50+
j i
51+
| |
52+
v v
53+
a b a b
54+
```
55+
56+
第4步
57+
```
58+
maxLength: 2
59+
set: [b, a] -> [b, a, b] -> [a, b]
60+
61+
j i
62+
| |
63+
v v
64+
a b a b
65+
66+
j i
67+
| |
68+
v v
69+
a b a b
70+
```
71+
72+
## 参考

src/leetcode/leetcode-3/__tests__.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Author: Rainy
3+
* @Date: 2020-02-06 19:05:18
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2022-01-23 16:08:25
6+
*/
7+
8+
import { lengthOfLongestSubstring } from '.';
9+
10+
test('Test lengthOfLongestSubstring', () => {
11+
expect(lengthOfLongestSubstring('abcabcbb')).toBe(3);
12+
expect(lengthOfLongestSubstring('bbbbb')).toBe(1);
13+
expect(lengthOfLongestSubstring('pwwkew')).toBe(3);
14+
expect(lengthOfLongestSubstring('')).toBe(0);
15+
});

src/leetcode/leetcode-3/index.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: Rainy
3+
* @Date: 2019-11-14 19:25:01
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2022-01-23 16:07:42
6+
*/
7+
8+
export function lengthOfLongestSubstring(s: string) {
9+
const set = new Set();
10+
let maxLength = 0;
11+
let i = 0;
12+
let j = 0;
13+
14+
for(; i < s.length; i++) {
15+
if (!set.has(s[j])) {
16+
maxLength++;
17+
} else {
18+
while (set.has(s[i])) {
19+
set.delete(s[j]);
20+
j++;
21+
}
22+
}
23+
24+
set.add(s[i]);
25+
maxLength = Math.max(maxLength, set.size);
26+
}
27+
28+
return maxLength;
29+
};

0 commit comments

Comments
 (0)