leetcode Contains Duplicate II

水题
题目链接这里

public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashSet<Integer> myHashSet=new HashSet<Integer>();
        int i;
        k=Math.min(nums.length, k);
        for(i=0;i<nums.length;i++)
        {
            if(myHashSet.contains(nums[i]))
            {
                return true;

            }
            else
            {
                myHashSet.add(nums[i]);
                if(i-k>=0)
                {
                    myHashSet.remove(nums[i-k]);
                }
            }
        }       
        return false;
    }
}

你可能感兴趣的:(leetcode Contains Duplicate II)