simple C++ solution,29ms,without Map


  • 0
    W
    class Solution {
    public:
        int findPairs(vector<int>& nums, int k) {
            int ret = 0, numslen = nums.size(), start = 0;
            sort(nums.begin(), nums.end());
            for (int i = 0; i < numslen - 1; i++) {
                if (i == 0 || nums[i] != nums[i - 1]) {
                    for (int j = max(start, i) + 1; j < numslen; j++) {
                        if (nums[j] - nums[i] == k) {
                            ret++;
                            start = j;
                            break;
                        }
                        else if(nums[j] - nums[i] > k) break;
                    }
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

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