class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size = s.size();
if(size < 2)
{
return size;
}
unordered_set<char> charset;
int slow = 0, fast = 0;
int max_len = -1;
while(fast < size)
{
while (charset.find(s[fast]) != charset.end())
{
charset.erase(s[slow++]);
}
charset.insert(s[fast]);
max_len = max(max_len, fast - slow + 1);
fast++;
}
return max_len;
}
};
LeetCode3. 无重复字符的最长子串
最新推荐文章于 2024-01-11 19:22:57 发布