Use unordered_map is much faster than map


  • 0
    C
    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) 
        {
            unordered_map<int, int> record;
            
            for(int i=0; i<nums.size(); i++)
            {
                if(record.count(nums[i]) && i - record[ nums[i] ] <= k )
                    return true;
                else
                    record[ nums[i] ] = i;
            }
            
            return false;
        }
    };

Log in to reply
 

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