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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • 您可以通过以下联系方式联系我们: 邮箱:qywy_yztp@qq.com
    iosapp客服中心阅读 226评论 0 0
  • 走一走,动一动,跑一跑,来球击球,高球杀球,短球切球,反转,漏洞,对角,博弈智取
    被猫叼着的鱼阅读 234评论 0 0
  • 我是丹尼尔,一个过气的小说家,真可惜,在《岛上书店》的故事里,也许还算不上男二号。 亲身经历车祸的一刹那,我才想到...
    单眼皮婷阅读 579评论 2 4