Skip to content

Instantly share code, notes, and snippets.

@kingsamadesu
Created December 3, 2020 20:31
Show Gist options
  • Save kingsamadesu/30315cac3fcad4474729fe93bf48ecda to your computer and use it in GitHub Desktop.
Save kingsamadesu/30315cac3fcad4474729fe93bf48ecda to your computer and use it in GitHub Desktop.
219. Contains Duplicate II (leetcode.com)
/*
Your runtime beats 98.60 % of java submissions.
Your memory usage beats 71.96 % of java submissions.
*/
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
k = Math.min(k,nums.length);
Set<Integer> set =new HashSet<Integer>();
for (int j = 0 ; j < k ; j++){
if(!set.add(nums[j])){return true;}
}
for(int i = k ; i < nums.length ; i++){
if(!set.add(nums[i])){return true;}
set.remove(nums[i-k]);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment