题解 | #最长无重复子串# 滑动窗口,用set维护一个不重复的窗口
最长无重复子串
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
- 滑动窗口,用set维护一个不重复的窗口
/* 滑动窗口,用set维护一个不重复的窗口 */ public int maxLength (int[] arr) { int res = 0; Set<Integer> set = new HashSet<>(); for(int l = 0, r = 0; r < arr.length; r++) { int a = arr[r]; while(set.contains(a)) { set.remove(arr[l++]); } set.add(a); res = Math.max(res, r - l + 1); } return res; }