219. Contains Duplicate II 发表于 2022-01-19 12345678910111213141516171819class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { // 窗口中元素数量:k + 1, eg: k = 1, window length = 2, when i = 1, start remove Set<Integer> set = new HashSet<>(); for (int i = 0; i < nums.length; i++) { if (set.contains(nums[i])) { return true; } set.add(nums[i]); if (i - k >= 0) { set.remove(nums[i - k]); } } return false; }} Reference219. Contains Duplicate II