32ms c++ solution unordered_map


  • 0
    T
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        if(nums.size()<=1) return false;
        unordered_map<int, int> m;
        
        for(int i = 0; i < nums.size(); i++){
            auto it = m.find(nums[i]);
            if(it!=m.end()){
                if(i - it->second <=k) return true;
                it->second = i;
            }else
                m[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.