尺取法
public static int lengthOfLongestSubstring(String s) {
if(s==null || s.length()==0)
return 0;
HashSet<Character> set = new HashSet<Character>();
int max = 0;
int i = 0;
int j = 0;
while(j<s.length())
{
if(set.contains(s.charAt(j)))
{
if(max<j-i)
{
max = j-i;
}
while(s.charAt(i)!=s.charAt(j))
{
set.remove(s.charAt(i));
i++;
}
i++;
}
else
{
set.add(s.charAt(j));
}
j++;
}
max = Math.max(max,j-i);
return max;
}