Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
Example 1:
1 | Input: [3, 1, 4, 1, 5], k = 2 |
Example 2:
1 | Input:[1, 2, 3, 4, 5], k = 1 |
Example 3:
1 | Input: [1, 3, 1, 5, 4], k = 0 |
Note:
- The pairs (i, j) and (j, i) count as the same pair.
- The length of the array won’t exceed 10,000.
- All the integers in the given input belong to the range: [-1e7, 1e7].
分类讨论
当k = 0时, 问题等价于寻找数组中出现次数大于等于2次的元素个数, 建立一个hashmap统计出现次数即可.O(n)
当k < 0时, 我本以为k=-1和k=1的结果应该一样, 毕竟集合{(i, j): k == i - j}
和集合{(i, j): -k == i - j}
的元素个数相同,但是这个题的坑在于k<0时, 应该返回结果是0, 而example并未说明!
当k>0时, 同一个元素只需要考虑一次,不需要考虑这个元素出现了几次(出现1次和出现多次没有区别). 所以先进行排序和去重
设置双指针, i=0,j=1, tmp = nums[j] - nums[i],
若tmp == k,则说明已经找到一组解, 再令++j, ++i去寻找下一个可能的解
若tmp<k 说明需要++j, 以获得更大的差值nums[j] - nums[i]
若tmp>k 说明需要++i, 以获得更小的差值nums[j] - nums[i]
1 | class Solution { |