二刷3. Longest Substring Without Repeating Characters

Medium
基础的two pointer题目

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0){
            return 0;
        }   
        HashSet<Character> set = new HashSet<>();
        int l = 0;
        int r = 0;
        int maxLen = 0;
        while (l < s.length() && r < s.length()){
            if (!set.contains(s.charAt(r))){
                set.add(s.charAt(r));
                maxLen = Math.max(maxLen, r - l + 1);
                r++;
            } else {
                set.remove(s.charAt(l));
                l++;
            }     
        }
        return maxLen;
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/8bbe4420599a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞