class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int len=nums.size();
bool flag=false;
multimap<int ,int> mulm;
for(int i=0;i<len;i++)
{
mulm.insert(make_pair(nums[i],i));
}
multimap<int,int>::iterator it1=mulm.begin();
multimap<int ,int>::iterator it2=it1;
it2++;
for(;it2 !=mulm.end();it1++,it2++)
{
if((it2->first ==it1->first)&& (it2->second - it1->first<=k))
{
flag=true;
return true;
}
}
return false;
}
};
时间: 2024-10-23 22:50:40