题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
import java.util.*; public class Solution { /** * * @param arr int整型一维数组 the array * @return int整型 */ public int maxLength(int[] arr) { int len = arr.length; if(len <= 1) return 1; HashSet set = new HashSet(); int left = 0, right = 1; int maxLen = 1; set.add(arr[left]); while (right < len && left < right) { if (set.contains(arr[right])) { while (left < right && set.contains(arr[right])) { set.remove(arr[left]); left++; } } set.add(arr[right]); right++; int tempMax = right - left; maxLen = Math.max(tempMax, maxLen); } return maxLen; } }