zoukankan      html  css  js  c++  java
  • 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), where i and j are both numbers in the array and their absolute difference is k.

    Example 1:

    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.

    Example 2:

    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).

    Example 3:

    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].

    本题是寻找数组中差为k的数对的个数

    方法一

     1     public int findPairs(int[] nums, int k) {
     2        if (nums == null || nums.length == 0 || k < 0)   return 0;
     3         
     4         Map<Integer, Integer> map = new HashMap<>();
     5         int count = 0;
     6         for (int i : nums) {
     7             map.put(i, map.getOrDefault(i, 0) + 1);
     8         }
     9         
    10         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    11             if (k == 0) {
    12                 //count how many elements in the array that appear more than twice.
    13                 if (entry.getValue() >= 2) {
    14                     count++;
    15                 } 
    16             } else {
    17                 if (map.containsKey(entry.getKey() + k)) {
    18                     count++;
    19                 }
    20             }
    21         }
    22         
    23         return count;
    24     }

    方法二

     1     public  int findPairs1(int[] nums, int k) {
     2         if(k<0 || nums.length<=1){
     3             return 0;
     4         }
     5 
     6         Arrays.sort(nums);
     7         int count = 0;
     8         int left = 0;
     9         int right = 1;
    10 
    11         while(right<nums.length){
    12             int firNum = nums[left];
    13             int secNum = nums[right];
    14             if(secNum-firNum<k)
    15                 right++;
    16             else if(secNum - firNum>k)
    17                 left++;
    18             else{
    19                 count++;
    20                 while(left<nums.length && nums[left]==firNum){
    21                     left++;
    22                 }
    23                 while(right<nums.length && nums[right]==secNum){
    24                     right++;
    25                 }
    26 
    27             }
    28             if(right==left){
    29                 right++;
    30             }
    31         }
    32         return count;
    33     }
  • 相关阅读:
    DOM编程
    BOM编程
    JavaScript
    CSS
    HTML入门
    shiro与项目集成开发
    shiro授权测试
    散列算法
    shiro认证流程
    spring boot 入门及示例
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7669992.html
Copyright © 2011-2022 走看看