532. K-diff Pairs in an Array
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)
, wherei
andj
are both numbers in the array and their absolute difference isk
.
给一个数组, 返回所有的k-diff
组合.
例子:
[3, 1, 4, 1, 5], k = 2
=> 2
k-diff
组合为(1, 3), (5, 3)
[1, 2, 3, 4, 5], k = 1
k-diff
组合为(1, 2), (2, 3), (3, 4), (4, 5)
思路
排序后从左往右扫描数组, 只看右边能组成k-diff
的数字.
Code
1 | def kDiff(nums, k): |
##时间复杂度
$O(nlogn) + O(n)$
EOF