C++ 7 lines O(1) space solution


  • 0
    Z
        int findPairs(vector<int>& nums, int k) {
            int count=0;
            sort(nums.begin(),nums.end());
            for(int i=0;i<nums.size();i++){
                if(find(nums.begin()+i+1,nums.end(),nums[i]+k)!=nums.end()) count++;
                while(nums[i+1]==nums[i]) i++;
            }
            return count;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.