532. K-diff Pairs in an Array 阵列中的K-diff对

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:

  1. The pairs (i, j) and (j, i) count as the same pair.
  2. The length of the array won‘t exceed 10,000.
  3. All the integers in the given input belong to the range: [-1e7, 1e7].

题意:求出数组中差值为k的数字对个数

解法:用哈希表储存每个数字的出现次数

  1. class Solution(object):
  2. def findPairs(self, nums, k):
  3. """
  4. :type nums: List[int]
  5. :type k: int
  6. :rtype: int
  7. """
  8. if k < 0: return 0
  9. count = 0
  10. d = dict()
  11. for num in nums:
  12. if(num in d):
  13. d[num] = d[num] + 1
  14. else:
  15. d[num] = 1
  16. for item in d:
  17. if k == 0 :
  18. if d[item] >= 2:
  19. count = count + 1
  20. else:
  21. if item+k in d:
  22. count = count + 1
  23. return count

来自为知笔记(Wiz)

时间: 2024-10-19 07:54:07

532. K-diff Pairs in an Array 阵列中的K-diff对的相关文章

[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 sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

查找数列中第K小的元素(C语言版)

今天在看<算法:C语言实现>时,在快速排序那一章最后一节讲述了利用快速排序的思想,快速排序每次划分后在枢轴的左边的元素都比枢轴小,在枢轴右边的数都比枢轴大(或相等),而划分后枢轴本身就放在了(有序时)它自身应该在的位置,在每次划分后判断枢轴下标和k的大小就可以快速找出数列中第k小的数了. 看完之后,我想既然利用快速排序的思想可以很快的找到第k小的数,那么能不能利用计数排序的思想来查找第k小的数呢,仔细一想,完全可以!计数排序是利用一个计数数组C来记录待排序数组中各个不同数值出现的次数,然后通过

LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 求二叉树中第k个最小的元素,中序遍历就可以了,具体代码和另一个Binary Tree Iterator差不多其实,这题由于把=写成了==调bug调了好久,细心细心啊啊

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log(m + n)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

挑战面试编程:查找数组中第k大的数

查找数组中第k大的数 问题: 查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思路: 1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. 只需找到第k大的数,不必把所有的数排好序.我们借助快速排序中partition过程,一般情况下,在把所有数都排好序前,就可以找到第k大的数.我们依据的逻辑是,经过一次partition后,数组被pivot分成左右两部分:S左.S右.当S左的元素个数|S左|等于k-1时,pivo

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 k-diff pair is defined as an integer pair (i, j), where i and j 

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 the array and their absolute difference is k. Exa

[Leetcode] DP--300. 629. K Inverse Pairs Array

Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. We define an inverse pair as following: For ith and jth element in the array, if i < j and a[i] > a[j] then it's a

629. K Inverse Pairs Array

Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. We define an inverse pair as following: For ith and jth element in the array, if i < j and a[i] > a[j] then it's a