14ms Java Solution Using Map

  • 0
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++)
            Integer last = map.put(nums[i], i);
            if (last != null && Integer.valueOf(last) >= i-k)
                return true;
        return false;

  • 0

    I think you may use intValue() instead of valueOf(), and when last equal to null should be considered.

Log in to reply

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