C++中memset()函数的用法详解
https://blog.csdn.net/lyj2014211626/article/details/65481630
解题思路
遍历字符串,记录左边一个标志位st,st到当前都没有重复字母,每次更新ans即可。 pos用来记录上一次字符的位置,用于更新st
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s=="")
return 0;
int pos[300];
memset(pos,-1,sizeof(pos));
int ans=1;
int st=-1;
for(int i=0;i<s.length();i++){
if(pos[s[i]]==-1){
pos[s[i]]=i;
ans=max(ans,i-st);
}else{
st=max(st,pos[s[i]]);
ans=max(ans,i-st);
pos[s[i]]=i;
}
}
return ans;
}
};
作者:bluegrey-t
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/geng-xin-zuo-bian-biao-zhi-wei-by-bluegr-kdfu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。