leetcode - 3:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// 哈希集合,记录每个字符是否出现过
unordered_set<char> occur;
int end = -1;
int ans = 0;
for(int i=0;i<s.length();i++){
if(i!=0){
// 删除队头
occur.erase(s[i-1]);
}
// 没有超过字符串长度的范围内 && s[i]这个字符在当前子串中没有出现过
while( end+1<s.length() && !occur.count(s[end+1]) ){
// 队尾插入
end++;
occur.insert(s[end]);
}
ans = ans > end-i+1 ?ans:end-i+1;
}
return ans;
}
};
原文:https://www.cnblogs.com/baboon/p/12968582.html