一、题目内容

image.png

二、题解

解法1:

思路

双指针,标记左右游动窗口
set当做队列使用,发现右侧新入栈重复,则remove

代码

  1. public class Solution {
  2. public int maxLength(int[] arr) {
  3. // write code here
  4. Set<Integer> set = new HashSet<>();
  5. int max = 0;
  6. int left = 0, right = 0;
  7. while (right < arr.length) {
  8. while (set.contains(arr[right])) {
  9. set.remove(arr[left++]);
  10. }
  11. set.add(arr[right++]);
  12. max = Math.max(max, right - left);
  13. }
  14. return max;
  15. }
  16. }