a short c++ version using unordered_map,beats 92% soloution


  • 0
    X

    bool containsNearbyDuplicate(vector<int>& nums, int k) {
    unordered_map<int,int> exist;
    int i = 0;
    for(;i < nums.size();++i){
    if(exist.count(nums[i]) ){
    if(abs(exist[nums[i]] - i)<=k )
    return true;
    else
    exist[nums[i]] = i;
    }
    else
    exist.insert(pair<int,int>(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.