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 };
  • 相关阅读:
    【BZOJ】1710: [Usaco2007 Open]Cheappal 廉价回文
    【BZOJ】3302: [Shoi2005]树的双中心 && 2103: Fire 消防站 && 2447: 消防站
    【BZOJ】1706: [usaco2007 Nov]relays 奶牛接力跑
    【Atcoder】CODE FESTIVAL 2017 qual A D
    【BZOJ】3038: 上帝造题的七分钟2 && 3211: 花神游历各国
    【BZOJ】1707: [Usaco2007 Nov]tanning分配防晒霜
    【BZOJ】1754: [Usaco2005 qua]Bull Math
    【BZOJ】1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
    【BZOJ】1828: [Usaco2010 Mar]balloc 农场分配(经典贪心)
    【BZOJ】1709: [Usaco2007 Oct]Super Paintball超级弹珠
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6656302.html
Copyright © 2011-2022 走看看