https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

    1. /**
    2. * 法一: 滑动窗口
    3. */
    4. public int lengthOfLongestSubstring(String s) {
    5. if (s == null || s.length() == 0) {
    6. return 0;
    7. }
    8. char[] str = s.toCharArray();
    9. //记录遍历过的字符
    10. Set<Character> set = new HashSet<>();
    11. int len = Integer.MIN_VALUE;
    12. //滑动窗口
    13. int R = -1;
    14. for (int L = 0; L < s.length(); L++) {
    15. if (L != 0) {
    16. set.remove(str[L - 1]);
    17. }
    18. // R往右扩到不能再扩
    19. while (R + 1 < s.length() && !set.contains(str[R + 1])) {
    20. 、、 set.add(str[++R]);
    21. }
    22. len = Math.max(len, R - L + 1);;
    23. }
    24. return len;
    25. }