Solution URL
https://leetcode.com/submissions/detail/421118946/
代码
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
//Time: O(N)
//Space: O(N)
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
if(map.containsKey(nums[i])){
int diff = i - map.get(nums[i]);
if(diff <= k) return true;
}
map.put(nums[i], i);
}
return false;
}
}