LeetCode/532. K-diff Pairs in an Array.md
2018-09-09 17:27:43 +08:00

54 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [532. K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/description/)
# 思路
题目求一个数组中两个元素的值之差的绝对值为k的元素对数首先考虑用map记录一个值是否出现但是为了满足不重复计数map中的值应该分成好几种情况容易出错。
如果将数组进行排序则清晰许多,因为排序后前面的元素肯定不大于后面的元素,很容易满足不重复计数的条件。
排序后可以用map来标记一个值是否出现不过也可以考虑用**滑动窗口**的方法这样就不用使用额外的空间而且也比map查找快。
首先考虑k不为0的情况设置滑动窗口的左右指针left和right初始为0和1。为了避免重复计数将左指针右移直到不能右移(即nums[left]!=nums[left+1]), 同理右指针也应该进行同样的移动。
然后再判断nums[left] + k 和 nums[right] 的大小关系并进行合理的更新操作,具体见代码。
注意点:
* k = 0时上述方法会失效不过提前增加一个判断即可见代码。
* 没说输入的k一定非负但k应该满足非负才有意义所以当k小于0时直接返回0即可。
# C++
```
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
if(k < 0) return 0;
sort(nums.begin(), nums.end());
int count = 0;
if(k == 0){
/* 这种方法较下面的方法慢一些,因为判断多一些?
int i = 0;
while(i < nums.size()){
while(i < nums.size() - 1 && nums[i+1] == nums[i]) i++;
if(i != 0 && nums[i] == nums[i-1]) count++;
i++;
}
*/
int tag = 0;
for(int i = 1; i < nums.size(); i++){
if(nums[i] == nums[i - 1]){
if(tag == 0) count++;
tag = 1;
}
else tag = 0;
}
return count;
}
int left = 0, right = 1;
while(right < nums.size()){
while(left < nums.size() - 1 && nums[left+1] == nums[left]) left++; // 左指针右移直到不能右移
while(right < nums.size() - 1 && nums[right+1] == nums[right]) right++; // 右指针右移直到不能右移
if(nums[left] + k == nums[right]){
count++;
left++;
right++;
}
else if(nums[left] + k < nums[right]) left++;
else right++;
}
return count;
}
};
```