题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
题意:找出无重复字符的最长子串
思路:
方法一:滑动窗口,用HashSet判断字符是否存在,右端先增,无法增时左端增
方法二:遍历一遍,维护每个字符出现的最右位置
代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans=0;
int a[300];
memset(a,-1,sizeof(a));
int len=s.length();
int l=0;
for(int i=0;i<len;i++)
{
l=max(l,a[s[i]]+1);
ans=max(ans,i-l+1);
a[s[i]]=i;
}
return ans;
}
};