Description
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.
Solution
|
|
Analyse
这里就是简单的用了一个map,用list存储相同数字的位数,那么就可以进行判断,复杂度到了O(n ^ 2)。
Optimization
First
|
|
Analyse
这个方法不错,主要利用了set.add方法的返回值,而且用set构造了一个k大小的滑动窗口,感觉很赞,时间复杂度O(n)。
Second
|
|
Analyse
这个map比我用的好太多,直接在map生成过程中判定,很简单。