leetcode第三题:无重复字符的最长子串

public static int lengthOfLongestSubstring(String s) {
        int len = s.length();
        int res = 0;
        int start = 0;
        int end = 0;
        HashSet set = new HashSet();
        while (start < s.length() && end < s.length()) {
            if (set.contains(s.charAt(end))) {
                set.remove(s.charAt(start));
                start++;
            }
            else {
                set.add(s.charAt(end));
                end++;
                res = Math.max(res, end - start);
            }
        }
        return res;
    }

猜你喜欢

转载自www.cnblogs.com/pusan/p/12219841.html