题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
import java.util.*; public class Solution { /** * * @param arr int整型一维数组 the array * @return int整型 */ public int maxLength (int[] arr) { // write code here Set<Integer> set = new HashSet<>(); int pre = 0; int maxLen = 0; for (int i = 0; i < arr.length; i++) { if (set.contains(arr[i])) { if (maxLen < set.size()) { maxLen = set.size(); } // 去除 arr[i] 前的所有的数 while (set.contains(arr[i])) set.remove(arr[pre++]); set.add(arr[i]); } else { set.add(arr[i]); } } return Math.max(maxLen, set.size()); } }