给定一个整数数组和一个整数 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. 13-1-return

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. js input框限制输入为数字并限制长度

    <input type="number" name="price" id="priceVal" placeholder="请 ...

  3. java_IO流(输入流)

    * 字节输入流(InputStream):所有字节输入流的父类 * 所有子类共性方法: * int read():从输入流中读取数据的下一个字节 * int read(byte[] b):从输入流中拂 ...

  4. Maven使用初步

    一.安装: 1.下载:http://maven.apache.org/download.cgi 2.设置环境变量 1.创建"M2_HOME",值为Maven安装路径(如:D:\Ja ...

  5. loj6244 七选五

    题意:从n个数中选k个数,问有多少种排列与标准k项串恰好有x个位置相同. 标程: #include<cstdio> using namespace std; typedef long lo ...

  6. PKUSC2019 D2T2

    PKUSC2019 D2T2 把n(n<=100)的树(无边权)放在m维空间上(坐标都是整点),使得任意两个点的曼哈顿距离都是原树上的距离 求最小的m,并给出构造方案 性质好题,巧妙构造题. 原 ...

  7. [原创]新版PageOffice V4.0为什么用弹出窗口的方式打开文件?

    前的包含文档处理功能的Web办公系统,在打开文档的时候,一部分系统是采用Office文档嵌入到主窗口页面中右侧工作区域的方式,另一部分系统采用的是弹出新的浏览器窗口,里面完整的嵌入Office文件的打 ...

  8. 2019-8-30-C#-如何在项目引用x86-x64的非托管代码

    title author date CreateTime categories C# 如何在项目引用x86 x64的非托管代码 lindexi 2019-08-30 08:53:52 +0800 20 ...

  9. [NOI2003]逃学的小孩【观察+树的直径】

    Online Judge:Bzoj1509,Luogu P4408 Label:观察,树的直径 题目描述 输入 第一行是两个整数N(\(3≤N≤200000\))和M,分别表示居住点总数和街道总数.以 ...

  10. No context type was found in the assembly

    如果解决方法中有多个项目存在,记住要在默认项目中选择你需要的项目进行 enable-migrations    add-migration 以及updatebase