/*
nums 存入 hashmap中 key是数字 value是位置 循环时判断 如果存在key 则判断位置差 小于k的话 return true 否则 更新位置;
*/
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(nums[i])) {
if(i - (int)map.get(nums[i]) <= k) {
return true;
}
}
map.put(nums[i], i);
}
return false;
}
}Contains Duplicate II
Contains Duplicate II leetcode java 走地牙
最新推荐文章于 2024-03-28 23:22:33 发布