详见:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
C++:
class Solution { public: int findPairs(vector<int>& nums, int k) { int res = 0, n = nums.size(); unordered_map<int, int> m; for (int num : nums) { ++m[num]; } for (auto a : m) { if (k == 0 && a.second > 1) { ++res; } if (k > 0 && m.count(a.first + k)) { ++res; } } return res; } };
参考:http://www.cnblogs.com/grandyang/p/6545075.html