Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
class Solution {
public:
bool containsNearbyDuplicate(
vector<int>& nums,
int k) {
int size = nums.size();
map<int, int> m;
for(
int i =
0; i < size; ++i){
if(m.count(nums[i])){
if(i - m[nums[i]] <= k)
return true;
}
m[nums[i]] = i;
}
return false;
}
};
转载请注明原文地址: https://ju.6miu.com/read-662166.html