class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s=="")
return 0;
int n=s.size();
int start,end=0;
int maxLength=0;
unordered_set<char>Lookfor;
while(end<n){
char cur=s[end];
if(!Lookfor.count(cur)){
Lookfor.insert(cur);
maxLength=max(maxLength,end-start+1);
end++;
}
else{
Lookfor.erase(s[start]);
start++;
}
}
return maxLength;
}
};
为什么会报错越界,把 Lookfor.insert(cur);
maxLength=max(maxLength,end-start+1);这两行换下顺序就可执行不是很理解