题目地址:
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
题目的意思很简单,也很好理解,就是找到字符串中最长且不重复的子串的长度。
方法一:
思路:遍历字符串,从第一个开始,然后往后查找,如果找到重复的,记录下当前不重复子串的长度,退出当前循环,情况共计数器,然后从第二个开始进行遍历,重复这个过程,直到整个字符串结束,完成最长子串的查找。
int lengthOfLongestSubstring(string s)
{
std:set<char> sub;
int count = 0, max = 0;
for(int i = 0; i < s.size(); i ++) {
for(int j = i; j < s.size(); j++) {
if(sub.find(s[j]) == sub.end())
{
count ++;
sub.insert(s[j]);
if(count > max) {
max = count;
}
}
else {
break;
}
}
count = 0;
sub.clear();
}
return max;
}