219. Contains Duplicate II
HashMap
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i)
{
if (i > k && m[nums[i - k - 1]] < i - k + 1)
{
m.erase(nums[i - k - 1]);
}
if (m.count(nums[i])) return true;
m[nums[i]] = i;
}
return false;
}
};
- T:
- S: