首页 > 代码库 > 219. Contains Duplicate II

219. Contains Duplicate II

 1 class Solution {
 2 public:
 3     bool containsNearbyDuplicate(vector<int>& nums, int k) {
 4         map<int, int> m;
 5         for (size_t i = 0; i < nums.size(); ++i) {
 6             if (m.find(nums[i]) != m.end() && i - m[nums[i]] <= k) return true;
 7             else m[nums[i]] = i;
 8         }
 9         return false;
10     }
11 };

 

219. Contains Duplicate II