2020-07-03
1. Title description
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
2. Problem solution
3. Code
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len=s.size();
if (!len) return 0;
int f[128];
memset(f,0xff,sizeof(f));
int tmp=0,res=0;
for (int i=0;i<len;i++){
if (i-f[s[i]]>tmp) tmp=tmp+1;
else tmp=i-f[s[i]];
res=max(tmp,res);
f[s[i]]=i;
}
return res;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len=s.size();
if (!len) return 0;
int f[128];
memset(f,0xff,sizeof(f));
int i=-1,res=0;
for (int j=0;j<len;j++){
if (f[s[j]]!=-1){
i=max(i,f[s[j]]);
}
res=max(res,j-i);
f[s[j]]=j;
}
return res;
}
};