当出现重复字符,以它后面的字符为开始进行初始化

import java.util.*;

public class Solution {
    public int lengthOfLongestSubstring (String s) {
        // write code here
        Set<Character> set = new HashSet<>();
        int i,j,ans,t;
        char c;
        
        i = j = ans = t = 0;//j表示开头,i表示结尾
        while(i < s.length()){
            c = s.charAt(i);
            if(set.contains(c)){
                while(s.charAt(j) != c)set.remove(new Character(s.charAt(j++)));  
                j++;
                t = i-j+1;
            } else {
            	t++;
            	set.add(c);
            }
            ans = Math.max(ans,t);
            i++;
        }
        return ans;
    }
}