A 32ms Solution Using Unordered_map


  • 0
    Y
    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_map<int,int> myMap;
            for(int i = 0; i < nums.size(); i++) {
                if(myMap.find(nums[i]) != myMap.end()) if((i - myMap.find(nums[i])->second) <= k) return true;
                myMap[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.