idea:

设置一个hashset存储非重复元素
j:遍历s
i:最近的一个非重复指针

注意点:

1.Set set = new HashSet<>();
2. add remove

public int lengthOfLongestSubstring(String s) {
        int i = 0, j = 0, max = 0;
        Set<Character>  set = new HashSet<>();
        
        while(j < s.length()){
            if(! set.contains(s.charAt(j)) ){
                set.add(s.charAt(j++));
                max = Math.max(max, set.size());
            }else{
                set.remove(s.charAt(i++));
            } 
        }
        return max;
    }

相关文章:

  • 2021-11-20
  • 2022-01-10
  • 2021-05-17
  • 2022-02-05
  • 2021-12-25
  • 2021-10-31
猜你喜欢
  • 2021-09-05
  • 2021-11-10
  • 2021-10-03
  • 2021-06-04
  • 2021-09-12
  • 2021-12-23
  • 2021-09-14
相关资源
相似解决方案