-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_longest_sub_string.cpp
More file actions
34 lines (32 loc) · 867 Bytes
/
Copy path03_longest_sub_string.cpp
File metadata and controls
34 lines (32 loc) · 867 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
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s.empty()) {
return 0;
}
int max_sub = 1;
int left = 0;
int right = 0;
string sub_str = s.substr(left, right - left + 1);
while (right < s.size()) {
if (sub_str.find(s[right]) == (sub_str.size() - 1)) {
max_sub = max(max_sub, right - left + 1);
right++;
} else {
left++;
}
sub_str = s.substr(left, right - left + 1);
}
return max_sub;
}
};
int main() {
string str{""};
Solution solu;
cout << "result: " << solu.lengthOfLongestSubstring(str) << endl;
// cout << "try: " << str[0] << str[1] << str.find(str[0])<< endl;
return 0;
}