Java 14ms with HashMap only


  • 0
    J

    public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
    if (nums.length < 2 || nums == null){
    return false;
    }
    HashMap<Integer, Integer> number = new HashMap<Integer, Integer>();
    for (int i = 0; i < nums.length; i++){
    if (!number.containsKey(nums[i])){
    number.put(nums[i], i);
    }else if (i - number.get(nums[i]) <= k){
    return true;
    }else {
    number.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.