给定一个整数数组和一个整数 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, 2, 3, 4, 5], k = 1 输出: 4 解释: 数组中有四个 1-diff 数对, (1, 2), (2, 3), (3, 4) 和 (4, 5)。

示例 3:

输入: [1, 3, 1, 5, 4], k = 0 输出: 1 解释: 数组中只有一个 0-diff 数对,(1, 1)。

注意:

  1. 数对 (i, j) 和数对 (j, i) 被算作同一数对。
  2. 数组的长度不超过10,000。
  3. 所有输入的整数的范围在 [-1e7, 1e7]。
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
map<pair<int, int>, int> check;
int len = nums.size();
int res = 0;
for(int i = 0; i < len; i++)
{
for(int j = i + 1; j < len; j++)
{
if(abs(nums[j] - nums[i]) == k && check[make_pair(nums[i], nums[j])] == 0)
{
res++;
check[make_pair(nums[i], nums[j])] = 1;
check[make_pair(nums[j], nums[i])] = 1;
}
}
}
return res;
}
};

Leetcode532.K-diff Pairs in an Array数组中的K-diff数对的更多相关文章

  1. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  2. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  3. [LeetCode] K-diff Pairs in an Array 数组中差为K的数对

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  4. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. 532 K-diff Pairs in an Array 数组中差为K的数对

    详见:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ C++: class Solution { public: ...

  6. 【LeetCode】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...

  7. 数组中的k个最小值

    问题:输入n个整数,找出其中最小的k个数. 方案一:将输入的n个整数进行排序,输出前k个数即为所求的k个最小数.时间复杂度为O(nlogn). 方案二:创建一个大小为k的容器,来存储最小的k个数.遍历 ...

  8. 寻找数组中第K大数

    1.寻找数组中的第二大数 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  9. 前端算法题:找出数组中第k大的数字出现多少次

    题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...

随机推荐

  1. <数据可视化>样例+数据+画图

    1 样例 1.1样例1 子图系列 from pylab import * def f(x): return np.exp(-x) * np.cos(2*np.pi*x) x1 = np.arange( ...

  2. SQL语句的四种连接

    SQL的四种连接查询 内连接 inner join 或者 join 外连接 左连接   left join 或者 left outer join 右连接  right join 或者 right ou ...

  3. 一个mdl相关的问题

    感觉挺有意义的,细节问题 http://www.kernelmode.info/forum/viewtopic.php?f=14&t=4318这个人代码到底犯了什么错误 I want to p ...

  4. vue项目导出EXCEL功能

    因为一些原因导出EXCEL功能必须前端来做,所以就研究了一下,在网上也找了一些文章来看,有一些不完整,我做完了就记录下来,供大家参考: 1.首先先安装依赖: npm install file-save ...

  5. java笔试之放苹果

    题目描述:M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输入:每个用例包含二个整数M和N.0<=m< ...

  6. centos zabbix4.0编译安装

    zabbix的部署原理 zabbix server需要把监控数据入sql数据库,所以得Mysql环境 zabbix的web是基于php开发的,所以得LNMP环境 部署zabbix server和zab ...

  7. spring:bean的作用范围和生命周期

    bean的作用范围调整: <!--bean的作用范围调整 bean标签的scope属性: 作用:用于指定bean的作用范围 取值:常用的就是单例的和多例的 singleton:单例的(默认值) ...

  8. springboot与任务(定时任务)

    描述: 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler ...

  9. JS里面function和Function的区别

    js里Function 与 function的不一样的,不仅仅是大小写的问题. 简单点说:大写的Function是一个类 ,而小写的function是一个对象. Function是一个构造器,func ...

  10. 转:五种I/O模型和select函数简介

    源地址:http://blog.csdn.net/jnu_simba/article/details/9070955 一.五种I/O模型 1.阻塞I/O 我们在前面所说的I/O模型都是阻塞I/O,即调 ...