滑动窗口

与力扣 3. 无重复字符的最长子串 类似

import java.util.*;


public class Solution {
    /**
     * 
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public int maxLength (int[] arr) {
        // write code here
        if(arr.length <= 1) return arr.length;
        int len = arr.length;
        int maxLen = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int start = 0, end = 0; end < len; end++) {
            if (map.containsKey(arr[end])) {
                start = Math.max(start, map.get(arr[end]) + 1);
            }
            maxLen = Math.max(maxLen, end - start + 1);
            map.put(arr[end], end);
        }
        return maxLen;
    }
}