LeetCode-532:K-diff Pairs in an Array (给定绝对值差的值对个数)

【C# + HALCON 机器视觉】HALCON经典算子:光流法运动分析(optical_flow_mg) 摘要:本文面向智能监控人流统计需求,研究C#与HALCON集成开发中optical_flow_mg算子的应用。通过RTSP获取视频流,利用光流法计算运动矢量,结合形态学操作与区域筛选实现行人区域分割,最终完成人流量统计与热力图可视化。详细阐述ROI局部处理等性能优化策略、try-catch异常捕获机制及跨平台集成方案。实验表明,该系统检测准确率达93.6%,单帧处理时间42ms,为公共场所人流量统计提供了完整可行的技术方案。 阅读详情

Question

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的元素值对(i, j),返回元素值对的数量。

Answer

Solution 1:

HashMap实现。

  • 由题目可以知道,同LeetCode-1:Two Sum类似,题中在map中找到nums[i]+k,就相当于找到了一对符合题目的值对;
  • 首先给出没有值对,即符合条件的值对数量为0的情况;
  • 将nums中的所有元素以 (value:number) 的map对对象,存储在map中;
  • 遍历map中的元素,注意区分k为0和不为0的情况。
class Solution {
    public int findPairs(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k < 0) return 0;

        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;
        for (int i : nums){
            map.put(i, map.getOrDefault(i, 0) + 1);
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()){
            if (k == 0){
                if (entry.getValue() >= 2){
                    count++;
                }
            }else{
                if (map.containsKey(entry.getKey() + k)){
                    count++;
                }
            }
        }
        return count;
    }
}
  • Runtime: 30 ms
  • Beats 58.44 % of java submissions
  • 时间复杂度:O(n);空间复杂度:O(n)
ardupilot开发 --- 避障篇 适配ardupilot的雷达避障功能开发,使用低成本雷达思岚科技系列雷达,配置简单,灵敏度高,障碍物检测范围不低于30m。 阅读详情

相关推荐

ADS8681数据手册没讲透的细节:SPI时序、量程切换与电压换算的避坑指南

本文深入解析ADS8681数据手册未提及的SPI时序优化、量程切换陷阱与电压校准技巧。针对树莓派5与ADS8681的SPI通信问题,提供CPOL/CPHA配置建议;详解量程切换的位操作玄机与寄存器配置流程;并提出三步校准法应对非理想供电环境,帮助开发者提升ADC测量精度。

weixin_29178069的博客 522

[LeetCode解题报告] 532. 数组中的 k-diff 数对

哈希或者排序+双指针

liuliangcan的博客 192

ROS相机内参标定详细步骤指南

相机标定的目的是为了确定相机内部参数和外部参数,以便修正图像并将图像坐标映射到世界坐标,或者从世界坐标映射到图像坐标。这个过程是计算机视觉和三维感知任务的关键步骤之一,它有以下主要目的: 1. **减小畸变**:相机镜头和传感器可能引入径向和切向畸变,使得图像中的直线变得弯曲或者物体的形状不准确。通过标定相机,可以矫正这些畸变,使图像更准确。

m0_56661101的博客 1万+

LeetCode刷题:532.数组中的K-diff数对

题目描述: 看到这个题目第一眼就:排序+回溯+剪枝,统计最后结果数 好消息是根据这个思路写出来了,成功解题 坏消息是因为下标问题卡了一个小时,压死骆驼的最后一根稻草 看到官方解答,官方思路:排序+二分查找,自闭了排序+回溯+剪枝思路如下: 我寻思这个相当与从数列中挑选两个数的全排列吧,把一对一对的组合全写出来,然后挑选合格的,剪枝操作可以去除一下重复的数组以及明显val值会大于K的数组。这样呢,剪枝之后会比暴力求解时间复杂度低一些。之前看到一个人说过,回溯和暴力循环差不多,只是回溯通过剪枝的技巧,去除一些

一直特立独行的猫 231

数字IC面试核心:从MUX基础到Glitch-Free时钟切换电路深度剖析

本文深入剖析数字IC设计中的MUX基础原理及其在Glitch-Free时钟切换电路中的应用。从二选一MUX的门级实现到时钟切换中的毛刺问题,详细讲解了同步切换机制和工程优化技巧,帮助工程师掌握高速时钟电路设计的核心要点。

weixin_42629815的博客 327

大白映射 宝马Esys编程设码映射、USB设备映射

大白映射,宝马Esys设码编程、USB设备映射

denson0394的博客 4803

Leetcode 532. 数组中的K-diff数对 C++

Leetcode 532. 数组中的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, 5)。 尽管数组中有两个1,但我们只应返回不同的数对的数量。 示例 2: 输入:[1,

weixin_43750513的博客 240

UE5热更新(通过Pak包加载关卡资源,并添加到流关卡)

请先阅读我上一篇文章)**

xialuhui的博客 6456

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

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

SK_Jaco的博客 442

Android 蓝牙——概述(一)

Profiles (蓝牙应用规范),它从应用场景的角度为蓝牙技术的使用制定了不同的规范。这也是和大众日常生活接触最多的一部分。蓝牙支持很多Profiles:MAP:(MessageAccessProfile),短信访问协议,允许设备间的信息交换,短信。使用场景:智能车载中同步短信等信息。HFP:(Hands-free Profile),电话免提协议,让蓝牙设备(如蓝牙耳机/车机)可以控制电话,如接听、挂断、拒接等。

小旭的专栏 3492

2025高教社数学建模国赛A题 - 烟幕干扰弹的投放策略(完整参考论文)

摘要:本研究基于模拟遗传退火算法优化无人机投放烟幕弹策略,建立三维运动学模型分析遮蔽效果。针对不同战术场景(单机单弹至多机多弹),构建以遮蔽时间最大化为目标的非线性优化模型。通过改进遗传算法求解最优参数组合,结果显示在给定条件下单枚烟幕弹对M1导弹的有效遮蔽时长可达1.4秒。研究为无人机协同干扰提供了高效解决方案,验证了智能算法在复杂战术决策中的适用性。关键词:烟幕干扰;无人机投弹;运动学建模;时序优化;遗传算法。

ProminentWu的博客 544

LeetCode 532. 数组中的 k-diff 数对

给你一个整数数组nums和一个整数k,请你在数组中找出k-diff 数对,并返回不同的的数目。数对定义为一个整数对|val|表示val的绝对值。2数组中有两个 2-diff 数对, (1, 3) 和 (3, 5)。尽管数组中有两个 1 ,但我们只应返回不同的数对的数量。4数组中有四个 1-diff 数对, (1, 2), (2, 3), (3, 4) 和 (4, 5)。1数组中只有一个 0-diff 数对,(1, 1)。

m0_56090828的博客 656

4、LTE无人机认证:保障通信安全的新方案

本文针对LTE网络中无人机通信存在的安全风险,提出了一种基于可信第三方GCS的新型认证协议。该协议通过非对称加密和哈希函数保护IMSI等敏感信息,防止身份泄露、中间人攻击、拒绝服务攻击和位置追踪等威胁。利用Scyther工具对协议进行形式化分析,验证了其在机密性、一致性和活性等方面的安全属性,证明该方案能有效提升无人机在LTE网络中的通信安全性与隐私保护能力。

pink的博客 61

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

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

Made in Code 900

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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...

珍妮的选择的博客 293

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

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

mine_song的博客 1862

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【leetcode】532. 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的博客 883

【Leetcode】532. K-diff Pairs in an Array

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

筱葭的博客 4088

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逆天耗子 641

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的博客 136

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 ...

想当厨子的程序媛 299

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的博客 235

[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 146

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

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

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

独钓寒江雪 401

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的博客 584
上一篇: LeetCode-1:Two Sum (固定和的元素的索引)
下一篇: LeetCode-88:Merge Sorted Array (合并两排序数组)
大树先生的博客
博客等级 码龄9年 2692粉丝 200原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值