[LeetCode] 532. K-diff Pairs in an Array

Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array.

A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:

  • 0 <= i, j < nums.length
  • i != j
  • nums[i] - nums[j] == k

Notice that |val| denotes the absolute value of val.

Example 1:

Input: nums = [3,1,4,1,5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.

Example 2:

Input: nums = [1,2,3,4,5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).

Example 3:

Input: nums = [1,3,1,5,4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).

Constraints:

  • 1 <= nums.length <= 104
  • -107 <= nums[i] <= 107
  • 0 <= k <= 107

数组中的K-diff数对。

给你一个整数数组 nums 和一个整数 k,请你在数组中找出 不同的 k-diff 数对,并返回不同的 k-diff 数对 的数目。

k-diff 数对定义为一个整数对 (nums[i], nums[j]) ,并满足下述全部条件:

0 <= i, j < nums.length
i != j
nums[i] - nums[j] == k
注意,|val| 表示 val 的绝对值。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/k-diff-pairs-in-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给一个整数数组和一个整数 K,请你求出数组中有多少对数字满足两个数字之间的差值是 K。

思路是遍历两遍数组,第一遍用 hashmap 记录每个数字和他们出现的次数。第二次遍历数组,也要分两种情况讨论

  • 如果 K = 0,那么只要找到任何一个出现超过两次的数字,就res++
  • 如果 K > 0,那么当遍历到 num[i] 的时候,就去看 hashmap 中是否有 nums[i] + k,有则res++

对于第二种情况,我第一次做的时候有试图去找 nums[i] - k,后来发觉没必要。因为 hashmap 会遍历所有的key,所以结果不会丢失

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int findPairs(int[] nums, int k) {
 3         // corner case
 4         if (nums == null || nums.length == 0) {
 5             return 0;
 6         }
 7 
 8         // normal case
 9         HashMap<Integer, Integer> map = new HashMap<>();
10         int res = 0;
11         for (int num : nums) {
12             map.put(num, map.getOrDefault(num, 0) + 1);
13         }
14 
15         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
16             if (k == 0) {
17                 if (entry.getValue() >= 2) {
18                     res++;
19                 }
20             } else {
21                 if (map.containsKey(entry.getKey() + k)) {
22                     res++;
23                 }
24             }
25         }
26         return res;
27     }
28 }

 

[2023.1] 三年后二刷,发觉代码短了好多,也没有去想是否需要考虑 nums[i] - k 的问题了。

 1 class Solution {
 2     public int findPairs(int[] nums, int k) {
 3         HashMap<Integer, Integer> map = new HashMap<>();
 4         for (int num : nums) {
 5             map.put(num, map.getOrDefault(num, 0) + 1);
 6         }
 7         
 8         int count = 0;
 9         for (int num : map.keySet()) {
10             if (k == 0 && map.get(num) >= 2) {
11                 count++;
12             }
13             if (k != 0 && map.containsKey(num + k)) {
14                 count++;
15             }
16         }
17         return count;
18     }
19 }

 

two sum题目总结

LeetCode 题目总结

posted @ 2020-04-27 06:44  CNoodle  阅读(189)  评论(0编辑  收藏  举报