无重复字符的最长子串

 


class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(); if(n<=1){ return n; } int l = 0, r = 1; HashSet<Character> set = new HashSet<>(); set.add(s.charAt(l)); int res = 0; while(r<n){ while(r<n && !set.contains(s.charAt(r))){ set.add(s.charAt(r)); r++; } res = Math.max(res, r-l); set.remove(s.charAt(l)); l++; } return res; } }
posted @ 2024-07-06 18:47  northli  阅读(2)  评论(0编辑  收藏  举报