Posted on

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

  1. https://leetcode-cn.com/problems/contains-duplicate-ii/solution/cun-zai-zhong-fu-yuan-su-ii-by-leetcode/

Leave a Reply

Your email address will not be published. Required fields are marked *