3. Longest Substring Without Repeating Characters(medium)

3. 无重复字符的最长子串


class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set set = new HashSet<>();
        int left = 0;
        int right = 0;
        int res = 0;
        while (right < s.length()) {
            if (!set.add(s.charAt(right))) {
                while (s.charAt(left) != s.charAt(right)) {
                    set.remove(s.charAt(left));
                    left++;
                }
                left++;
            }
            res = Math.max(res, right - left + 1);
            right++;
        }
        return res;
    }
}

你可能感兴趣的:(3. Longest Substring Without Repeating Characters(medium))