Created
December 3, 2020 20:31
-
-
Save kingsamadesu/30315cac3fcad4474729fe93bf48ecda to your computer and use it in GitHub Desktop.
219. Contains Duplicate II (leetcode.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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