leetcode3.无重复字符的最长字串
采用滑动窗口方法
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n=s.size();
if(n==0)
return 0;
int result=0;
unordered_set<char> set;
set.insert(s[0]);
for(int i=0,j=0;i<n;i++){
while(j+1<n&&set.find(s[j+1])==set.end()){
set.insert(s[j+1]);
j++;
}
result=max(result,j-i+1);
set.erase(s[i]);
}
return result;
}
};