-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
47 lines (41 loc) · 861 Bytes
/
3.cpp
File metadata and controls
47 lines (41 loc) · 861 Bytes
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
#include <iostream>
#include <unordered_set>
using namespace std;
int lengthOfLongestSubstring(string s) {
if (s.empty()) {
return 0;
}
int left = 0;
int right = 0;
int max_len = 1;
int n = s.size();
unordered_set<int> dic;
while (right < n) {
if (dic.count(s[right])) {
dic.erase(s[left]);
++left;
} else {
dic.insert(s[right]);
++right;
max_len = max(max_len, right - left);
}
}
return max_len;
}
void test() {
string s = "abcabcbb";
int ret;
ret = lengthOfLongestSubstring(s);
cout << ret << endl;
s = "bbbbb";
ret = lengthOfLongestSubstring(s);
cout << ret << endl;
s = "pwwkew";
ret = lengthOfLongestSubstring(s);
cout << ret << endl;
}
int main()
{
test();
return 0;
}