-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003_LSC.cpp
49 lines (41 loc) · 1.25 KB
/
003_LSC.cpp
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map <int, int> dict_sub;
int n = s.size();
if (n == 1)
{
return 1;
}
int left = 0;
int len_sub = 0;
for (int right = 0; right < n; right++)
{
if(dict_sub.find(s[right]) != dict_sub.end())
{
left = max(left, dict_sub[s[right]] + 1);
}
len_sub = max(len_sub, right - left + 1);
dict_sub[s[right]] = right;
}
return len_sub;
}
};
int main(){
Solution Sol1;
cout << Sol1.lengthOfLongestSubstring("abcabcbb") << endl; //3
cout << Sol1.lengthOfLongestSubstring("bbbb") << endl;//1
cout << Sol1.lengthOfLongestSubstring("pwwkew") << endl;//3
cout << Sol1.lengthOfLongestSubstring("dvdf") << endl; //3
cout << Sol1.lengthOfLongestSubstring("") << endl; //0
cout << Sol1.lengthOfLongestSubstring(" ") << endl;//1
cout << Sol1.lengthOfLongestSubstring("tmmzuxt") << endl;//5
return 0;
}