[leetcode]219. Contains Duplicate II
Analysis
ummmm~—— [ummmm~]
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.
输入一个数组,找出重复元素的下标差,如果差值不超过k,则返回true,否则返回false。
Implement
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int, int> mymap;
int len = nums.size();
int t;
for(int i=0; i<len; i++){
if(mymap.find(nums[i]) != mymap.end() && i-mymap[nums[i]]<=k)
return true;
else
mymap[nums[i]] = i;
}
return false;
}
};