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

这道题首先二话不说用O(

n2
)的解法秒掉,使用set:
class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        const int size = nums.size();
        if(size == 0 || k < 0)
            return 0;

        set<pair<int, int>> s;
        int a[2] = {0};
        for(int i=0; i<size-1; ++i){
            for(int j=i+1; j<size; ++j){
                if(nums[i] - nums[j] == k || nums[j] - nums[i] == k){
                    a[0] = nums[i];
                    a[1] = nums[j];
                    if(a[0] > a[1])
                        std::swap(a[0], a[1]);
                    s.insert(make_pair(a[0], a[1]));
                }
            }
        }
        return s.size();
    }
};

不过有更好的O(n)的解法,使用unordered_set,不过就是要注意k=0的情况要单独写出来,只有同一个数出现多次才被计算入结果。

Newton & Nvidia 开源 GPU 加速机器人仿真器入门教程(一) Newton 是一个 GPU 加速、可扩展和可微分的物理仿真引擎,专为机器人、研究和高级仿真工作流而设计。Newton 基于 NVIDIA Warp 并集成了 MuJoCo Warp,为用户和开发人员提供了高性能仿真、现代 Python API 和灵活的架构。 阅读详情

相关推荐

VMvare 桥接模式无法联网、没有未桥接的主机网络适配器,无法将网络更改为桥接状态。

很多程序员,整天沉浸在业务代码的 CRUD 中,业务中没有大量数据做并发,缺少实战经验,对并发仅仅停留在了解,做不到精通,所以总是与大厂擦肩而过。我把私藏的这套并发体系的笔记和思维脑图分享出来,理论知识与项目实战的结合,我觉得只要你肯花时间用心学完这些,一定可以快速掌握并发编程。《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!战项目源码讲义》点击传送门即可获取!**

q16974058的博客 4335

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 numbe

chenjn69的博客 285

MIT IDSS深度解析:跨学科融合与系统科学实践

麻省理工学院的IDSS(Institute for Data, Systems, and Society, IDSS)是一个致力于通过先进分析方法推动教育与研究的前沿机构。它将工程学、信息科学和数据科学的方法与社会科学的分析方法相结合,以应对复杂的社会挑战。

AllenLV的博客 2089

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

Chris_zhangrx的博客 744

【BAAS】基于supabase的知识库详细设计

为满足快速开发需要和减少开发及维护成本,使用BAAS(Backend-as-a-Service)模式可以很好的降低我们的后端维护成本,并提高前端对接效率。一般来说,BAAS是一种云服务模式,BaaS供应商会为提供一些通用的能力,例如用户身份验证、数据库管理、远程更新和推送通知(针对移动应用程序),以及云存储和自托管。BaaS最主流的是Google的firebase,具有相当完善的功能和集成方案,但他是收费的,而且在我们实际项目中,可能需要私有化部署,这时就需要一些开源的替代方案,例如主流的。

Ric的博客 1782

MES软件:Siemens Opcenter二次开发

或者在项目中右击“引用”(References),选择“添加引用”(Add Reference),浏览并选择SDK中的DLL文件。在“签名”(Signing)选项卡中,选择“为程序集签名”(Sign the assembly),并创建一个新的强名称密钥文件。在Visual Studio中,选择“测试”(Test)菜单中的“运行所有测试”(Run All Tests)。在Visual Studio中,选择“生成”(Build)菜单中的“发布”(Publish)。按照向导的提示,选择安装路径和组件。

kkchenjj的博客 1647

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

呜呜哈的博客 3068

Google 二次签名 导致 Facebook , Google 登录失败

google 授权登录

xq610928的博客 1957

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

AI绘画与医疗可视化:用AI生成医学插图的实践

医疗可视化是医学教育、临床沟通和学术研究的核心工具。传统医学插图依赖手工绘制或专业3D建模,存在制作周期长、成本高、更新迭代慢等问题。随着深度学习技术的发展,AI绘画工具(如Stable Diffusion、DALL-E)展现出强大的图像生成能力,但医疗领域对插图的解剖准确性、标注规范性和语义完整性有极高要求,通用AI模型难以直接应用。本文聚焦AI技术与医疗领域的交叉创新,系统阐述如何通过数据预处理、模型定制化训练和后处理校验,构建符合医疗标准的插图生成系统。

大模型应用工坊 1789

ANPC三电平拓扑双脉冲试验的原理与实施

ANPC三电平拓扑因损耗低、谐波小、应力均衡而广泛应用,但其存在短、长两类换流通道,寄生电感和开关应力差异显著。短通道尖峰小、可靠性高;长通道应力严苛。因此需分别制定两套独立双脉冲测试方案,针对不同时段和器件(如T1、T5、T6、T4及对应二极管)全面考核开关特性。

嵌入式老牛的博客 360

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

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

mine_song的博客 1863

SI案例分享--PCB通孔互连新技术的应用(VeCS)

本文介绍了VeCS的物理结构和制造过程,并通过全波仿真模拟和测量,分析了VeCS的电性能,并与传统的测量方法进行了比较。结果表明,由于有一个连续的参考平面,VeCS的TDR阻抗可以很容易地控制到目标阻抗,同时可以大大减轻信道上的串扰,的确是一种性能不错的PCB互连创新设计方案。

一只豌豆象的博客 2118

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

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

筱葭的博客 4088

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

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

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

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

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

Made in Code 900

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
上一篇: 459. Repeated Substring Pattern
下一篇: 383. Ransom Note
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值