-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
61 lines (51 loc) · 1.23 KB
/
Copy path3.cpp
File metadata and controls
61 lines (51 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <map>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ma = -1, l = 0;
map<char, int> mc;
for(int i = 0; i < s.size(); ++i)
{
if(mc.find(s[i]) == mc.end())
{
mc[s[i]] = i;
++l;
}
else
{
//更新记录
if(l > ma)
{
ma = l;
}
//语句顺序2
//l = i - mc[s[i]];
int len = i - mc[s[i]];
//截短
//1编程习惯...
//cout << i - l << "|" << mc[s[i]] << endl;
for(int j = i - l; j <= mc[s[i]]; ++j)
{
//cout << "erase:" << s[i] << endl;
mc.erase(s[j]);
}
//语句顺序1
//l = i - mc[s[i]];
l = len;
mc[s[i]] = i;
}
}
if(l > ma)ma = l;
return ma;
};
};
int main()
{
Solution s;
string in;
cin >> in;
cout << s.lengthOfLongestSubstring(in) << endl;
return 0;
}