代码:
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
时间: 2024-10-05 05:05:59