天天看點

[leetcode]532. K-diff Pairs in an Array[leetcode]532. K-diff Pairs in an Array

[leetcode]532. K-diff Pairs in an Array

Analysis

盜将行好聽~—— [嘻嘻~]

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.

尋找數組中內插補點為k的數值對。

Implement

class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        if(nums.size() ==  || k < )
            return ;
        map<int, int> mymap;
        for(auto num:nums){
            if(mymap.find(num) == mymap.end())
                mymap[num] = ;
            else
                mymap[num]++;
        }
        map<int, int>::iterator it;
        int res = ;
        for(it=mymap.begin(); it!=mymap.end(); it++){
            if(k == ){
                if(it->second >= )
                    res++;
            }
            else{
                if(mymap.find(it->first+k) != mymap.end())
                    res++;
            }
        }
        return res;
    }
};