代码:

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
//int gap = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
Integer j = map.put(nums[i], i);
if(j != null){
int diff = i - j;
if(diff <= k) return true;
}
}
return false;
}
}

 

HashMap 和 HashSet 相关函数的信息:

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

https://msdn.microsoft.com/en-us/library/bb353005(v=vs.110).aspx

posted on 2016-01-12 06:48  爱推理的骑士  阅读(81)  评论(0编辑  收藏  举报