思路:
滑动窗口
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ret=0,left=0,right=-1;
int rec[256]={0};
int n=s.size();
while(right+1<n){
if(rec[s[right+1]]==0){
rec[s[++right]]=1;
if(ret<right-left+1)
ret=right-left+1;
}
else{
rec[s[left++]]=0;
}
}
return ret;
}
};
网友评论