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]

    Solution 1:sort the array and use two pointers

     1 class Solution {
     2 public:
     3     int findPairs(vector<int>& nums, int k) {
     4         int res=0;
     5         sort(nums.begin(),nums.end());
     6         for (vector<int>::iterator it=nums.begin();it!=nums.end();it++){
     7             if (it!=nums.begin() && *(it-1)==*it) continue;
     8             for (vector<int>::iterator it2=it+1;it2!=nums.end();it2++){ 9                 if (*it2-*it==k) {res++;break;}10             }
    11         }
    12         return res;
    13     }
    14 };

    Solution 2: use hashmap<number,counts>, traverse the map, if k=0 and the counts of the number is >1, res++;if k>0 and the (current number+k) exists in the map, res++. It will save the time but use more space.

    note: 1. line4, use unoredered_map, unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

             2. line 7, use auto, http://blog.csdn.net/hushujian/article/details/43196589

             3. line 9, use m.count, Searches the container for elements whose key is k and returns the number of elements found. Because unordered_map containers do not allow for duplicate keys, this means that the function actually returns 1 if an element with that key exists in the container, and zero otherwise.

     1 class Solution {
     2 public:
     3     int findPairs(vector<int>& nums, int k) {
     4         unordered_map<int,int>m;
     5         int res=0,n=nums.size();
     6         for (int num:nums) ++m[num]; //the hash map<num:count>
     7         for (auto a:m){
     8             if (k==0 && a.second>1) res++;
     9             if (k>0 && m.count(a.first+k)) res++;
    10         }
    11         return res;
    12     }
    13 };
  • 相关阅读:
    Intent.ACTION_TIME_TICK 广播
    Android ContentObserver
    android:duplicateParentState属性解释
    Android CursorAdapter
    android AndroidManifest.xml 多个android.intent.action.MAIN (
    PreferenceActivity详解
    WORD和WPS中英文混合的内容间距离很大怎么办?
    Android 屏幕适配
    OC第四课
    PL/SQL联系oracle成功可以sql解决的办法是检查表的名称无法显示
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6656302.html
Copyright © 2011-2022 走看看