219. Contains Duplicate II

    xiaoxiao2021-03-25  104

    

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

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

    转载请注明原文地址: https://ju.6miu.com/read-14167.html

    最新回复(0)