LeetCode刷题之K-diff Pairs in an Array

Problem

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.

Example1

Input: [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.
Input:[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).
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).

Note:

  1. The pairs (i, j) and (j, i) count as the same pair.
  2. The length of the array won’t exceed 10,000.
  3. All the integers in the given input belong to the range: [-1e7, 1e7].
My Solution
class Solution {
    public void quickSort(int[] nums, int left, int right) {
       if (left > right) {
            return ;
       }
       int i = left, j = right, base = nums[left], temp = 0;
       while (i != j) {
            while (nums[j] >= base && i < j) {
                --j;
            }
            while (nums[i] <= base && i < j) {
                ++i;
            }
            if (i < j) {
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        nums[left] = nums[i];
        nums[i] = base;

        quickSort(nums, left, i - 1);
        quickSort(nums, i + 1, right);
        return ;
    }

    public int[] removeDuplicates(int[] nums) {
        int[] result = new int[2];
        if (nums.length == 0) result[0] = 0;
        int i = 0;
        int count = 0;
        for (int j = 1; j < nums.length; ++j) {
            if (nums[j] != nums[i]) {
                ++i;
                nums[i] = nums[j];
                if (count >= 1) {
                    result[1]++;
                }
                count = 0;
            } else {
                count++;
            }
        }
        if (count >= 1) {
            result[1]++;
        }
        result[0] = i + 1;
        return result;
    }

    public int findPairs(int[] nums, int k) {
        int count = 0, t = 0;
        quickSort(nums, 0, nums.length - 1);
        int[] result = removeDuplicates(nums);
        int len = result[0];
        if (k == 0) {
            return result[1];
        }
        for (int i = 0; i < len; ++i) {
            for (int j = i + 1; j < len; ++j) {
                if (Math.abs(nums[i] - nums[j]) == k) {
                    t++;
                }
            }
            if (t >= 2) {
                count += 2;
                t = 0;
            } else {
                count += t;
                t = 0;
            }
        }
        return count;
    }
}
Great Solution
public int findPairs(int[] nums, int k) {
    if (nums == null || nums.length == 0 || k < 0)   return 0;
    
    Map<Integer, Integer> map = new HashMap<>();
    int count = 0;
    for (int i : nums) {
        map.put(i, map.getOrDefault(i, 0) + 1);
    }
    
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        if (k == 0) {
            //count how many elements in the array that appear more than twice.
            if (entry.getValue() >= 2) {
                count++;
            } 
        } else {
            if (map.containsKey(entry.getKey() + k)) {
                count++;
            }
        }
    }
    
    return count;
}
    原文作者:Gandalf0
    原文地址: https://www.jianshu.com/p/0b4f907569d0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞