问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3716 访问。

给定一个整数数组和一个整数 k, 你需要在数组里找到不同的 k-diff 数对。这里将 k-diff 数对定义为一个整数对 (i, j), 其中 i 和 j 都是数组中的数字,且两数之差的绝对值是 k.

输入: [3, 1, 4, 1, 5], k = 2

输出: 2

解释: 数组中有两个 2-diff 数对, (1, 3) 和 (3, 5)。尽管数组中有两个1,但我们只应返回不同的数对的数量。

输入:[1, 2, 3, 4, 5], k = 1

输出: 4

解释: 数组中有四个 1-diff 数对, (1, 2), (2, 3), (3, 4) 和 (4, 5)。

输入: [1, 3, 1, 5, 4], k = 0

输出: 1

解释: 数组中只有一个 0-diff 数对,(1, 1)。

注意:

数对 (i, j) 和数对 (j, i) 被算作同一数对。

数组的长度不超过10,000。

所有输入的整数的范围在 [-1e7, 1e7]。


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.

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.

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

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3716 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 3, 1, 4, 1, 5 };
var res = FindPairs(nums, 2);
Console.WriteLine(res); Console.ReadKey();
} private static int FindPairs(int[] nums, int k) {
if(k < 0) return 0;
var set1 = new HashSet<int>();
var set2 = new HashSet<int>();
for(int i = 0; i < nums.Length; i++) {
//包含+k时直接添加到set2
if(set1.Contains(nums[i] + k)) {
set2.Add(nums[i]);
}
//包含-k时直接添加到set2
if(set1.Contains(nums[i] - k)) {
set2.Add(nums[i] - k);
}
//把所有值存入set1
set1.Add(nums[i]);
}
//返回2中的数量即为结果
return set2.Count();
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3716 访问。

2

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)的更多相关文章

  1. C#LeetCode刷题-树状数组

    树状数组篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 ...

  2. C#LeetCode刷题之#643-子数组最大平均数 I( Maximum Average Subarray I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3728 访问. 给定 n 个整数,找出平均数最大且长度为 k 的连 ...

  3. leetCode刷题(找出数组里的两项相加等于定值)

    最近被算法虐了一下,刷一下leetcode,找找存在感 如题: Given an array of integers, return indices of the two numbers such t ...

  4. C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...

  5. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

  6. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...

  7. leetcode刷题-94二叉树的中序遍历

    题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...

  8. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  9. C#LeetCode刷题-双指针

    双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.5% 中等 11 盛最多水的容器   43.5% 中等 15 三数之和   16.1% 中等 16 最接近的三数之和   3 ...

随机推荐

  1. 拦截器(Interceptor)中的invocation.invoke()是什么意思?

    拦截器(Interceptor)中的invocation.invoke()是什么意思? 最佳答案: invocation.invoke() 就是通知struts2接着干下面的事情 比如 调用下一个拦截 ...

  2. Ethical Hacking - GAINING ACCESS(5)

    Server Side Attack Install Metasploit cummunity/pro and active it. Create a new project for the targ ...

  3. html地图定位

    利用高德地图api实现简单的地图功能,包括定位,地图缩放,搜索,列表展示等等基础功能 <!doctype html> <html> <head> <meta ...

  4. ICPC North Central NA Contest 2018

    目录 ICPC North Central NA Contest 2018 1. 题目分析 2. 题解 A.Pokegene B.Maximum Subarrays C.Rational Ratio ...

  5. 微服务迁移记(五):WEB层搭建(3)-FreeMarker集成

    一.redis搭建 二.WEB层主要依赖包 三.FeignClient通用接口 以上三项,参考<微服务迁移记(五):WEB层搭建(1)> 四.SpringSecurity集成 参考:< ...

  6. Python列表函数和方法

    Python列表函数和方法: 函数: len(列表名): 返回列表长度 # len(列表名): # 返回列表长度 lst = [1,2,3,'a','b','c'] print("lst 列 ...

  7. PHP array_pad() 函数

    实例 返回 5 个元素,并将 "blue" 值插入到数组的新元素中: <?php$a=array("red","green");pri ...

  8. PHP array_udiff_assoc() 函数

    实例 比较两个数组的键名和键值(使用内建函数比较键名,使用用户自定义函数比较键值),并返回差集: <?phpfunction myfunction($a,$b){if ($a===$b){ret ...

  9. PHP pi() 函数

    实例 返回圆周率 PI 的值: <?phpecho(pi());?>高佣联盟 www.cgewang.com 定义和用法 pi() 函数返回圆周率 PI 的值. 提示:命名常量 M_PI ...

  10. Java注解与反射

    概要 本文主要是总结Java注解与反射的相关知识,加深自己对Java类动态语言的理解,同时为日后学习Spring打下基础. 注解: 什么是注解 Annotation的作用 不是程序本身,但是可以对程序 ...