2006. Count Number of Pairs With Absolute Difference K 發表於 2023-02-13 | 分類於 leetcode problemsolution12345678910111213141516171819class Solution {public: int countKDifference(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); unordered_map<int,int> mp; int count = 0; for(int n:nums) mp[n]++; int i=0, n=nums.size(); while(i<n){ // cout<<nums[i]<<" "; if(mp.count(nums[i]+k)) count+= mp[nums[i]]*mp[nums[i]+k]; while(i+1<n && nums[i]==nums[i+1]) i++; i++; } return count; }}; analysis time complexity O(nlogn) space complexity O(n)