Solution: Run time complexity is O(n), space complexity is O(n).
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMapmap = new HashMap<>(); for(int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i])) { int pre = map.get(nums[i]); if (i - pre <= k) { return true; } } map.put(nums[i], i); } return false; } }
No comments:
Post a Comment