剑指 Offer II 016. 不含重复字符的最长子字符串

剑指 Offer II 016. 不含重复字符的最长子字符串

注意本题中的包含空格也算在里面,因此采用声明数组128位来包含这些特殊的字符,保险的情况下可以声明成256

采用快慢指针,注意快指针++,慢指针减少

class Solution {
    public int lengthOfLongestSubstring(String str) {
        if(str == null || str.equals("")) return 0;
        int s = 0;
        int e = 0;
        int max = Integer.MIN_VALUE;
        int[] array = new int[128];
        while(e < str.length()) {
            while(array[str.charAt(e)] > 0) {
                array[str.charAt(s)]--;
                s++;
            }
            max = Math.max(max, e - s + 1);
            array[str.charAt(e)]++;
            e++;
        }
        max = Math.max(max, e - s);
        return max;
    }
}

你可能感兴趣的:(剑指 Offer II 016. 不含重复字符的最长子字符串)