题意:在整数数组里面找到两个相等的数,并且下标的差不大于k,如果存在就返回true,否则返回false
解题思路:
依然是HashMap的应用
代码
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer , Integer> map = new HashMap<Integer , Integer>();
int len = nums.length;
for(int i=0; i<len; i++){
if(map.containsKey(nums[i]) && (i-map.get(nums[i]))<=k){
return true;
}
map.put(nums[i],i);
}
return false;
}
}