Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78d7f36

Browse files
committed
0003
1 parent e072871 commit 78d7f36

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

‎0003_Longest_Substring_Without_Repeating_Characters.md‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,60 @@
22

33
#### (1)思路
44

5+
滑动构造子串,使子串内字母唯一,取子串的最大长度。
6+
第一次提交,出现错误,"tmmzuxt",预期5,输出4。原因是子串左侧没有动态删除元素。
7+
第二次提交,出现错误,"aabaab!bb",预期3,输出2。原因是子串右侧没有动态增补。
8+
第三次提交,成功。
9+
510
#### (2)实现
611

712
```cpp
813
class Solution {
914
public:
1015
int lengthOfLongestSubstring(string s) {
16+
string s1;
17+
string::iterator it1, it2;
18+
int len, len_max;
19+
20+
len = 0;
21+
it1 = s.begin();
22+
it2 = it1;
23+
24+
if(it2 == s.end())
25+
return 0;
26+
27+
it2++;
28+
len = 1;
29+
len_max = 1;
30+
s1 = s.substr(0, 1);
31+
32+
while(it2 != s.end()){
33+
if(-1 == s1.find(*it2)){
34+
s1.push_back(*it2);
35+
len++;
36+
len_max = (len > len_max)? len :len_max;
37+
}
38+
else{
39+
s1.push_back(*it2); //第二次提交忘加:不管有没有发现都要加进来,因为s1要滑动
40+
while(*it1++ != *it2)
41+
s1.erase(0, 1); //第一次提交忘加
1142

43+
s1.erase(0, 1); //第一次提交忘加
44+
len = it2 - it1 + 1;
45+
}
46+
47+
it2++;
48+
}
49+
50+
return len_max;
1251
}
1352
};
1453
```
1554

16-
17-
1855
| 提交结果 | 执行用时 (pctl%) | 内存消耗 (pctl%) | 语言 |
1956
|:---------|:-----------------|:-----------------|:-----|
20-
| 通过 | ms (%) | MB (%) | cpp |
57+
| 通过 | 24 ms (52.4%) | 9.4 MB (63.06%) | cpp |
2158

2259
#### (3)收获
60+
61+
对动态构造(滑动)的序列,头尾的动态增删细节一定要细心。

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[0001 两数之和][1],2020年02月28日
44
[0002 两数相加][2],2020年03月02日
5-
0003 无重复字符的最长子串
5+
[0003 无重复字符的最长子串][3],2020年03月05日
66
0004 寻找两个有序数组的中位数
77
0005 最长回文子串
88
0006 Z 字形变换
@@ -1262,3 +1262,4 @@ LCP_5 发 LeetCoin
12621262

12631263
[1]: https://github.com/ksd04/leetcode_alg_practice/blob/master/0001_Two_Sum.md
12641264
[2]: https://github.com/ksd04/leetcode_alg_practice/blob/master/0002_Add_Two_Numbers.md
1265+
[3]: https://github.com/ksd04/leetcode_alg_practice/blob/master/0003_Longest_Substring_Without_Repeating_Characters.md

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /