力扣
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int i = 1;
int pos = 0;
string sub;
sub = s.substr(0);
cout<<sub<<endl;
int max = 0;
int length = 1;
if (s.size() >= 1) {
while (s[i] != '\0') {
sub = s.substr(pos, length);
// cout << sub << endl;
if (sub.find(s[i]) == -1) {//在当前子串中没有
length++;
max = (max > length) ? max : length;
// cout << s[i]<<" "<<sub << " " << int(sub.find(s[i])) <<" pos="<<pos<<endl;
}
else {
// printf("****");
int p = sub.find(s[i]);
int flag = (i - p - pos >= i - pos) ? 1 : 0;
if (flag == 1) {
max = (max > length) ? max : length;
}
else {
max = (max > p + 1) ? max : p + 1;
}
length = i - p - pos;
pos = pos + p + 1;
// printf("@pos=%d p=%d i=%d length=%d max=%d\n", pos, p, i, length, max);
}
i++;
}
max = (max > length) ? max : length;
return max;
}
else return 0;
}
};