Description
Submission
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int, int> m; for(int l = 0, r = 0; r < nums.size(); ++r) { if(r - l > k) { m[nums[l++]]--; } m[nums[r]]++; if(m[nums[r]] > 1) return true; } return false; } };
Reference
- https://leetcode-cn.com/problems/contains-duplicate-ii/solution/cun-zai-zhong-fu-yuan-su-ii-by-leetcode/