C++ short solution with hashmap


  • 0
    X
    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_map<int, int> hnums;
    		for (int i = 0; i < (int)nums.size(); ++i) {
    			if (hnums[nums[i]] && i-hnums[nums[i]] + 1 <= k)
    				return true;
    			hnums[nums[i]] = i + 1;//avoid 0
    		}
    		return false;
        }
    };
    

Log in to reply
 

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