2006. 差的绝对值为 K 的数对数目
给你一个整数数组 nums
和一个整数 k
,请你返回数对 (i, j)
的数目,满足 i < j
且 |nums[i] - nums[j]| == k
。
|x|
的值定义为:
x >= 0
,那么值为 x
。x < 0
,那么值为 -x
。
示例 1:
输入:nums = [1,2,2,1], k = 1 输出:4 解释:差的绝对值为 1 的数对为: - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] - [1,2,2,1]
示例 2:
输入:nums = [1,3], k = 3 输出:0 解释:没有任何数对差的绝对值为 3 。
示例 3:
输入:nums = [3,2,1,5,4], k = 2 输出:3 解释:差的绝对值为 2 的数对为: - [3,2,1,5,4] - [3,2,1,5,4] - [3,2,1,5,4]
提示:
1 <= nums.length <= 200
1 <= nums[i] <= 100
1 <= k <= 99
原站题解
java 解法, 执行用时: 4 ms, 内存消耗: 42 MB, 提交时间: 2023-10-11 10:58:14
class Solution { public int countKDifference(int[] nums, int k) { int res = 0, n = nums.length; Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(); for (int j = 0; j < n; ++j) { res += cnt.getOrDefault(nums[j] - k, 0) + cnt.getOrDefault(nums[j] + k, 0); cnt.put(nums[j], cnt.getOrDefault(nums[j], 0) + 1); } return res; } }
cpp 解法, 执行用时: 8 ms, 内存消耗: 13.4 MB, 提交时间: 2023-10-11 10:57:59
class Solution { public: int countKDifference(vector<int>& nums, int k) { int res = 0, n = nums.size(); unordered_map<int, int> cnt; for (int j = 0; j < n; ++j) { res += (cnt.count(nums[j] - k) ? cnt[nums[j] - k] : 0); res += (cnt.count(nums[j] + k) ? cnt[nums[j] + k] : 0); ++cnt[nums[j]]; } return res; } };
golang 解法, 执行用时: 0 ms, 内存消耗: 3.9 MB, 提交时间: 2023-10-11 10:57:40
func countKDifference(nums []int, k int) int { res := 0 cnt := map[int]int{} for _, num := range nums { res += cnt[num - k] + cnt[num + k] cnt[num] += 1 } return res }
python3 解法, 执行用时: 48 ms, 内存消耗: 16 MB, 提交时间: 2023-10-11 10:56:15
class Solution: def countKDifference(self, nums: List[int], k: int) -> int: res = 0 cnt = Counter() for num in nums: res += cnt[num - k] + cnt[num + k] cnt[num] += 1 return res
javascript 解法, 执行用时: 64 ms, 内存消耗: 43.6 MB, 提交时间: 2023-10-11 10:55:59
/** * @param {number[]} nums * @param {number} k * @return {number} */ var countKDifference = function(nums, k) { let res = 0, n = nums.length; const cnt = new Map(); for (let j = 0; j < n; ++j) { res += (cnt.get(nums[j] - k) || 0) + (cnt.get(nums[j] + k) || 0); cnt.set(nums[j], (cnt.get(nums[j]) || 0) + 1); } return res; };
javascript 解法, 执行用时: 64 ms, 内存消耗: 41.3 MB, 提交时间: 2023-10-11 10:55:40
/** * @param {number[]} nums * @param {number} k * @return {number} */ var countKDifference = function(nums, k) { let res = 0, n = nums.length; for (let i = 0; i < n; ++i) { for (let j = i + 1; j < n; ++j) { if (Math.abs(nums[i] - nums[j]) == k) { ++res; } } } return res; };
cpp 解法, 执行用时: 16 ms, 内存消耗: 12.4 MB, 提交时间: 2023-10-11 10:55:22
class Solution { public: int countKDifference(vector<int>& nums, int k) { int res = 0, n = nums.size(); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (abs(nums[i] - nums[j]) == k) { ++res; } } } return res; } };
java 解法, 执行用时: 8 ms, 内存消耗: 41.7 MB, 提交时间: 2023-10-11 10:55:05
class Solution { public int countKDifference(int[] nums, int k) { int res = 0, n = nums.length; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (Math.abs(nums[i] - nums[j]) == k) { ++res; } } } return res; } }
python3 解法, 执行用时: 252 ms, 内存消耗: 15.8 MB, 提交时间: 2023-10-11 10:54:32
class Solution: def countKDifference(self, nums: List[int], k: int) -> int: ans, n = 0, len(nums) for i in range(n-1): for j in range(i+1, n): if abs(nums[i] - nums[j]) == k: ans += 1 return ans
golang 解法, 执行用时: 0 ms, 内存消耗: 3.1 MB, 提交时间: 2021-09-22 14:57:35
func countKDifference(nums []int, k int) int { ans, n := 0, len(nums) for i := 0; i < n-1; i++ { for j := i+1; j < n; j++ { if abs(nums[i] - nums[j]) == k { ans++ } } } return ans } func abs(x int) int { if x > 0 { return x } return -x }