import java.util.HashMap;
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i< nums.length;i++)
{
if(map.containsKey(nums[i])&&((i-map.get(nums[i])<=k)))
return true;
else
map.put(nums[i], i);
}
return false;
}
}
map是一个接口
而hashmap是它的一种实现方式
for this problem, if you use eclipse,you will find that we need a () after the fucthion “new”;
another should be pay attention is when I use eclipse as the edcitior and I found that map.get()’s para is key.
So we should put the array as <num[i],i>in the hash map we defined.
here is the code.