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.这道题目的意思是:输入一个整数数组和一个整数k,若是数组中存在相等的两个数,并且他们的位置差不超过k,那么返回true,不然返回false数组
int length = nums.length; if(length<=1) return false; Set<Integer> count = new HashSet<Integer>(); for(int i=0;i<length;i++){ if(i > k){ count.remove(nums[i-k-1]); } if(!count.add(nums[i])){ return true; } } return false;