532. K-diff Pairs in an Array Medium
1/**
2 * [532] K-diff Pairs in an Array
3 *
4 * Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array.
5 * A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:
6 *
7 * 0 <= i, j < nums.length
8 * i != j
9 * nums[i] - nums[j] == k
10 *
11 * Notice that |val| denotes the absolute value of val.
12 *
13 * Example 1:
14 *
15 * Input: nums = [3,1,4,1,5], k = 2
16 * Output: 2
17 * Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
18 * Although we have two 1s in the input, we should only return the number of unique pairs.
19 *
20 * Example 2:
21 *
22 * Input: nums = [1,2,3,4,5], k = 1
23 * Output: 4
24 * Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
25 *
26 * Example 3:
27 *
28 * Input: nums = [1,3,1,5,4], k = 0
29 * Output: 1
30 * Explanation: There is one 0-diff pair in the array, (1, 1).
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= nums.length <= 10^4
36 * -10^7 <= nums[i] <= 10^7
37 * 0 <= k <= 10^7
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/k-diff-pairs-in-an-array/
43// discuss: https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn find_pairs(nums: Vec<i32>, k: i32) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_532() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.