11ms Java Solution


  • 0
    V
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            int n = nums.length;
            if( n==0 || k==0 ) return false;
            Hashtable<Integer,Integer> hash = new Hashtable<Integer,Integer>();
            int  i = -1;
            while(++i < n) {
                if( (hash.get(nums[i]) != null) && (i-hash.get(nums[i]) <= k) )
                    return true;
                hash.put(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.