Skip to content

Commit 01e84e4

Browse files
committed
✨ feat: 无重复字符的最长子串
1 parent 0f98f8c commit 01e84e4

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
/**
4+
* 给定一个字符串,超出其中不含有重复字符的最长子串长度。
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
function LengthOfLongestSubstring(s) {
9+
// 定义一个空字符串用来记录当前子串
10+
var sub = '';
11+
// 定义一个值用来记录当前子串长度与最长子串长度
12+
var count = 0, result = 0;
13+
for (var _i = 0, s_1 = s; _i < s_1.length; _i++) {
14+
var n = s_1[_i];
15+
// 如果当前子串内不含此字符 则添加此字符
16+
if (sub.indexOf(n) === -1) {
17+
sub += n;
18+
count++;
19+
// 判断此时是否超过最大子串长度,如超过,则替换此时的最大子串长度
20+
result = count > result ? count : result;
21+
}
22+
else {
23+
//如果包含 则更新子串
24+
sub = sub.slice(sub.indexOf(n) + 1);
25+
sub += n;
26+
count = sub.length;
27+
}
28+
}
29+
return result;
30+
}
31+
exports.LengthOfLongestSubstring = LengthOfLongestSubstring;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 给定一个字符串,超出其中不含有重复字符的最长子串长度。
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
function LengthOfLongestSubstring(s: string): number {
7+
// 定义一个空字符串用来记录当前子串
8+
let sub = '';
9+
// 定义一个值用来记录当前子串长度与最长子串长度
10+
let count = 0,
11+
result = 0;
12+
for(let n of s) {
13+
// 如果当前子串内不含此字符 则添加此字符
14+
if (sub.indexOf(n) === -1) {
15+
sub += n;
16+
count++;
17+
// 判断此时是否超过最大子串长度,如超过,则替换此时的最大子串长度
18+
result = count > result ? count : result;
19+
} else {
20+
//如果包含 则更新子串
21+
sub = sub.slice(sub.indexOf(n)+1);
22+
sub += n;
23+
count = sub.length;
24+
}
25+
}
26+
return result;
27+
}
28+
29+
export {LengthOfLongestSubstring}

DataStructure/Test/String.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {LongestCommonPerfix} from '../String/LongestCommonPerfix'
22
import {CountBinarySubstrings} from '../String/CountBinarySubstrings'
33
import {SumBinary} from '../String/SumBinary'
4+
import {LengthOfLongestSubstring} from '../String/LengthOfLongestSubstring'
45

56
test('LongestCommonPerfix', () => {
67
expect(LongestCommonPerfix(["flower","flow","flight"])).toBe('fl')
@@ -13,4 +14,9 @@ test('CountBinarySubstrings', () => {
1314

1415
test('SumBinary', () => {
1516
expect(SumBinary("11", "1")).toBe("100")
17+
})
18+
19+
test('LengthOfLongestSubstring', () => {
20+
expect(LengthOfLongestSubstring('abcdabcdeabcdef')).toBe(6);
21+
expect(LengthOfLongestSubstring('dvdf')).toBe(3);
1622
})

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Questions on LeetCode
6666
- 最长公共前缀 | LeetCode [14]
6767
- 计数二进制子串 | LeetCode [696]
6868
- 二进制求和 | LeetCode [67]
69-
69+
- 无重复字符的最长子串 | LeetCode [3]
7070
## 算法
7171

7272
#### 时间复杂度

docs/ds/String.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,38 @@ function SumBinary (a: string, b: string):string {
131131
}
132132
```
133133

134+
## 无重复的最长子串
135+
136+
> 此部分代码在 LengthOfLongestSubstring.ts 中
137+
138+
给定一个字符串,请你找出其中不含有重复字符的 **最长子串** 的长度。本题用到了滑动窗口的方法。
139+
140+
```typescript
141+
/**
142+
* 给定一个字符串,超出其中不含有重复字符的最长子串长度。
143+
* @param {string} s
144+
* @return {number}
145+
*/
146+
function LengthOfLongestSubstring(s: string): number {
147+
// 定义一个空字符串用来记录当前子串
148+
let sub = '';
149+
// 定义一个值用来记录当前子串长度与最长子串长度
150+
let count = 0,
151+
result = 0;
152+
for(let n of s) {
153+
// 如果当前子串内不含此字符 则添加此字符
154+
if (sub.indexOf(n) === -1) {
155+
sub += n;
156+
count++;
157+
// 判断此时是否超过最大子串长度,如超过,则替换此时的最大子串长度
158+
result = count > result ? count : result;
159+
} else {
160+
//如果包含 则更新子串
161+
sub = sub.slice(sub.indexOf(n)+1);
162+
sub += n;
163+
count = sub.length;
164+
}
165+
}
166+
return result;
167+
}
168+
```

0 commit comments

Comments
 (0)