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
.
思路是,保存每个元素出现的位置在map中,如果在查看一个新的元素时在map中也能找到这个元素,说明它已经出现过,map中的值保存的是前一次出现的位置,两次位置之差如果小于k的话,就返回true。