Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
describe
Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] – nums[j]| == k.
The value of |x| is defined as:
- x if x >= 0.
- -x if x < 0.
Example 1:
Input: nums = [1,2,2,1], k = 1 Output: 4 Explanation: The pairs with an absolute difference of 1 are: - [1,2,2,1] - [1,2,2,1] - [1,2,2,1]Copy the code
Example 2:
Input: nums = [1,3], k = 3
Output: 0
Explanation: There are no pairs with an absolute difference of 3.
Copy the code
Example 3:
Input: nums = [3,2,1,5,4], k = 2 Output: 3 Explanation: The pairs with an absolute difference of 2 are: - [3,2,1,5,4] - [3,2,1,5,4]Copy the code
Note:
1 <= nums.length <= 200
1 <= nums[i] <= 100
1 <= k <= 99
Copy the code
parsing
According to the question, is given a list of integers nums, then gives an integer k, let we find index (I, j), and I < j, at the same time satisfy the | nums [I] – nums [j] | = = k, asked us how many of the index. The idea is very simple, for such a simple problem, nature is to solve the first violent law.
- Initialization result Result is 0, nums length is M
- The first loop (0, m-1), and the second loop (I +1, M) for each index I
- If nums[I]-nums[j] == k or nums[j]-nums[I] == k
- The result at the end of the iteration is the answer
answer
class Solution(object):
def countKDifference(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
result = 0
M = len(nums)
for i in range(M-1):
for j in range(i+1,M):
if nums[i]-nums[j] == k or nums[j]-nums[i] == k:
result += 1
return result
Copy the code
The results
Runtime: 264 ms, Faster than 15.12% of Python online submissions for Count Number of Pairs With Absolute Difference K. Memory Usage: Given in Python online submissions for Count Number of Pairs With Absolute Difference K.Copy the code
parsing
We can also use Python’s built-in functions itertools.combinations to solve this problem. We need to use the built-in functions to form various combinations of length 2, so we can call itertools.combinations(nums, 2) directly to get iterators. If the difference between the absolute values of two elements is judged to be K, the result result is added by one, and the result obtained at the end of the iteration is the answer.
Function examples:
Run [(a,b) for a,b in itertools.combinations([1,2,2,1], 2)]
[(1, 2), (1, 2), (1, 1), (2, 2), (2, 1), (2, 1)], we can see that the combination of elements in the list using the built-in function already has the order before and after, so we don’t have to worry about the problem of I <j
answer
class Solution(object):
def countKDifference(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
result = 0
for a,b in itertools.combinations(nums, 2):
if a-b ==k or b-a==k:
result += 1
return result
Copy the code
The results
Runtime: 196 ms, Faster than 57.56% of Python online submissions for Count Number of Pairs With Absolute Difference K. Memory Usage: Given in Python online submissions for Count Number of Pairs With Absolute Difference K.Copy the code
Original link: leetcode.com/problems/co…
Your support is my biggest motivation