Sliding windows based C++ solution


  • 3

    This solution is really similiar the Minmum Value In the Windows.

    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_set<int> s;
            if(k<=0)  return false;
            if(k>=nums.size())  k=nums.size()-1;
            
            for(int i=0; i<nums.size(); i++){
                if(i>k)  s.erase(nums[i-k-1]);
                if(s.find(nums[i])!=s.end())  return true;
                s.insert(nums[i]);
            }
            return false;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.