[LeetCode]532. K-diff Pairs in an Array

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

[LeetCode]532. K-diff Pairs in an Array

题目描述

这里写图片描述

思路

哈希,计数
如果k为负,直接返回0
如果k为0,即为求重复元素

代码

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        int res = 0;
        if (k < 0)
            return res;
        unordered_map<int, int> um;
        for (int num : nums)
            um[num]++;
        for (auto p : um) {
            if (k == 0) {
                if (p.second > 1) res += 1;
            }
            else {
                if (um.count(p.first + k)) res += 1;
            }
        }
        return res;
    }
};

int main() {
    vector<int> nums = { 1,2,3,4,5 };
    Solution s;

    cout << s.findPairs(nums, -1) << endl;

    system("pause");
    return 0;
}
leetcode解题之532. K-diff Pairs in an Array Java版 阅读详情

相关推荐

Leetcode532. K-diff Pairs in an Array

思路: 成对的值不分先后,所以先对nums进行排序。 用一个set存储出现过的值,用于后续判断是否某个值已经有值与其成对。 分为两种情况: (1)k==0,即找出值相等的对数。 再用一个sameSet存储所有已成对的值,避免同一个值加入结果多次。只有sameSet中不含该值,且set中包含了该值,才能加入结果。 (2)k!=0,即找出差的绝对值为k的对数。 只有set中不包含该值但包

筱葭的博客 4083

Leetcode——532. K-diff Pairs in an Array

题目原址 https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ 题目描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a ...

想当厨子的程序媛 295

leetcode532. K-diff Pairs in an Array

532. K-diff Pairs in an Arrayhttps://leetcode.com/problems/k-diff-pairs-in-an-array/#/description Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in th

xxxxzr的博客 880

LeetCode 532 K-diff Pairs in an Array

LeetCode 532 K-diff Pairs in an Array题目思路代码 题目 Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array. A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true: 0 <= i, j < num

weixin_43796689的博客 133

LeetCode //C - 532. K-diff Pairs in an Array

【代码】LeetCode //C - 532. K-diff Pairs in an Array

Made in Code 894

leetcode 532. K-diff Pairs in an Array

leetcode 532. K-diff Pairs in an Array    下面这种解法TLE  public class Solution { public int findPairs(int[] nums, int k) { if(nums==null||nums.length==0||k<0) return 0; int len = nums

独钓寒江雪 399

Leetcode 532 K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers

夜色之浓,莫过于黎明前的黑暗。 402

LeetCode 532. K-diff Pairs in an Array

问题描述Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers

YXZreo的专栏 375

K-diff Pairs in an Array leetcode 532

原来统计数组也很简单,只是要多多实践。

天天烤代码 683

Leetcode532.K-diff Pairs in an Array数组中的K-diff数对

给定一个整数数组和一个整数k, 你需要在数组里找到不同的k-diff 数对。这里将k-diff数对定义为一个整数对 (i, j), 其中i和j都是数组中的数字,且两数之差的绝对值是k. 示例 1: 输入: [3, 1, 4, 1, 5], k = 2 输出: 2 解释: 数组中有两个 2-diff 数对, (1, 3) 和 (3, ...

Ha12312的博客 233

LeetCode Python 532 K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in...

Shawn的博客 433

[Leetcode]532. K-diff Pairs in an Array

Given an array of integers and an integerk, you need to find the number ofuniquek-diff pairs in the array. Here ak-diffpair is defined as an integer pair (i, j), whereiandjare both numbe...

Nicole852217677的专栏 106

[leetcode]532. K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in...

Brett 144

leetcode题解-532. K-diff Pairs in an Array

题目:Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers

呜呜哈的博客 3063

[leetcode]: 532. K-diff Pairs in an Array

1.题目Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers

沐沐余风的专栏 387
上一篇: [LeetCode]155. Min Stack
下一篇: [LeetCode]414. Third Maximum Number
charon____
博客等级 码龄11年 3粉丝 202原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值