天天看点

滑动窗口--无重复字符的最长子串3

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0)return 0;
        Set<Character> set=new HashSet<Character>();
        char[] c=s.toCharArray();
        int res=0;
        int start=0;
        int end=0;
        while(end<c.length){
            char a=c[end];
            end++;
            while(set.contains(a)){
                char b=c[start];
                start++;
                set.remove(b);
            }
            set.add(a);
            res=Math.max(res,end-start);
        }
        return res;
    }
}