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

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

题目:

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 the array and their absolute difference is k.

Example 1:
Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Note:
The pairs (i, j) and (j, i) count as the same pair.
The length of the array won't exceed 10,000.
All the integers in the given input belong to the range: [-1e7, 1e7].

本题是寻找数组中差为k的数对的个数。首先我们想到的一种方式是将数组进行排序,然后嵌套循环遍历数组,这种方法有两个点需要注意,一是跳过重复的数字,二是对每个数字一旦找到则停止内循环,这样可以节省运行时间。代码入下:

    public static int findPairs(int[] nums, int k) {
        Arrays.sort(nums);
        int res = 0;
        for(int i=0; i<nums.length-1; i++){
            for(int j=i+1; j<nums.length; j++)
                if(nums[j] - nums[i] == k) {
                    res++;
                    break;
                }
            while(i<nums.length-1 && nums[i] == nums[i+1])
                i++;
        }
        return res;
    }

第二种方法是使用HashMap来保存数字信息,这样就省去了一次遍历的时间,代码效率提升很多。代码入下:

    public int findPairs(int[] nums, int k) {
        if (k < 0)   return 0;
        HashMap<Integer,Integer> freqmap = new HashMap<Integer,Integer>();
        int count = 0;
        for(int num:nums){
            int f = (int)freqmap.getOrDefault(num, 0)+1;
            freqmap.put(num, f);
        }
        for (Integer key : freqmap.keySet()) {
            int a = (int)key;
            int b = a + k;
            if(!freqmap.containsKey(b)) continue;
            int bfreq = freqmap.get(b);
            int minfreq = a==b ? 2:1;
            if(bfreq>=minfreq ){
                count++;
                freqmap.put(a,1);
            }
        }
        return count;
    }

第三种方法是效率最高的方法,击败了98%的用户。这种方法的主要思路就是先对数组排序,然后使用滑动窗口遍历数组,因为数组排序之后差是可以连续移动两个指针得到的==这种方法同样要对相同的数字进行排除。代码入下:

    public  int findPairs1(int[] nums, int k) {
        if(k<0 || nums.length<=1){
            return 0;
        }

        Arrays.sort(nums);
        int count = 0;
        int left = 0;
        int right = 1;

        while(right<nums.length){
            int firNum = nums[left];
            int secNum = nums[right];
            if(secNum-firNum<k)
                right++;
            else if(secNum - firNum>k)
                left++;
            else{
                count++;
                while(left<nums.length && nums[left]==firNum){
                    left++;
                }
                while(right<nums.length && nums[right]==secNum){
                    right++;
                }

            }
            if(right==left){
                right++;
            }
        }
        return count;
    }
Leetcode-532. 数组中的 k-diff 数对 链接 532. 数组中的 k-diff 数对 题目 给定一个整数数组和一个整数k,你需要在数组里找到 不同的k-diff 数对,并返回不同的 k-diff 数对 的数目。 这里将k-diff数对定义为一个整数对 (nums[i], nums[j]),并满足下述全部条件: 0 <= i < j < nums.length |nums[i] - nums[j]| == k 注意,|val| 表示 val 的绝对值。 示例 示例 1: 输入:nums = [3, 1,... 阅读详情

相关推荐

LeetCode 每日一题——532. 数组中的 k-diff 数对

532. 数组中的 k-diff 数对给你一个整数数组 nums 和一个整数 k,请你在数组中找出 不同的 k-diff 数对,并返回不同的 k-diff 数对 的数目。k-diff 数对定义为一个整数对 (nums[i], nums[j]) ,并满足下述全部条件:0 ...

SK_Jaco的博客 439

K-diff Pairs in an Array

leetcode532题,竞赛题,虽然标签是easy,结果我想了很长时间,而且代码相当繁琐,不过我这里没有使用hashset之类可以自动去重的工具,除了使用了map之外,其余的都是正常的逻辑了。一点点分析,首先,如果k是个负数,则可以直接返回0. 接下来k就要分情况了,分成k是0和不是0的情况,这样做确实很繁琐,但是可以通过测试样例。 使用一个字典记录已经用到的数,另一个字典记录绝对值差为k的

恒河沙无数 1349

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

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

mine_song的博客 1857

Leetcode532. K-diff Pairs in an Array

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

筱葭的博客 4083

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

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

Made in Code 894

532. K-diff Pairs in an Array*

532. K-diff Pairs in an Array* https://leetcode.com/problems/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...

珍妮的选择的博客 290

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 532. K-diff Pairs in an Array

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

i逆天耗子 637

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

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

532. K-diff Pairs in an Array

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

Love_Taylor的博客 582

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数对(k-diff-pairs-in-an-array

一 目录 不折腾的前端,和咸鱼有什么区别目录一 目录二 前言三 解题及测试四 LeetCode Submit五 解题思路六 进一步思考二 前言 难度:简单涉及知识:数组、...

weixin_41806099的博客 223

[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题解-283. Move Zeroes
下一篇: leetcode题解-287. Find the Duplicate Number
liuchongee
博客等级 码龄14年 1099粉丝 206原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值