4 无重复字符的最长子串

                                 无重复字符的最长子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> sets = new HashSet<>();
        int i =0, j =0;
        int result  = 0;
        while(i < n && j < n){
            if(!sets.contains(s.charAt(j))){
                sets.add(s.charAt(j++));
                result = Math.max(result,j-i);
            }else{
                sets.remove(s.charAt(i++));
            }
        }
        return result;
    }
}
发布了450 篇原创文章 · 获赞 22 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_41563161/article/details/105469788
今日推荐