LeetCode(Map)2006. Count Number of Pairs With Absolute Difference K

灰太狼 2023-09-24 16:37 121阅读 0赞

1.问题

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]
  • [1,2,2,1]

Example 2:

Input: nums = [1,3], k = 3
Output: 0
Explanation: There are no pairs with an absolute difference of 3.

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]
  • [3,2,1,5,4]

Constraints:

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
  • 1 <= k <= 99

2. 解题思路

方法1:

1.定义result初始为0
2.for循环,两数相减取绝对值,如果等于k,result基础上加1
3.返回result结果

方法2:

1.新建hashmap,定义result初始为0
2.for循环遍历
1)map中是否有key减去k,说明有和其相减的数值,如果有,map.get(nums[i]-k)为1,也就是在result基础上加1
2)map中是否有key加k,说明有和其相减的数值,如果有map.get(nums[i]+k)为1,也就是在result基础上加1
3.返回reuslt

方法3:

1.定义长度为101(有1-100的数字)的arr数组,定义count初始为0
2.遍历数组,将nums在i位置上相同个数+1
3.遍历arr数组,arr[i]*arr[i+k]是在i位置上出现的重复数字个数和i+k位置上出现的重复数字个数,乘积是两个数字搭配符合要求的个数

3. 代码

代码1:

  1. class Solution {
  2. public int countKDifference(int[] nums, int k) {
  3. int result =0;//1.定义result初始为0
  4. for (int i = 0; i < nums.length; i++) {
  5. //2.for循环,两数相减取绝对值,如果等于k,result基础上加1
  6. for(int j=i+1; j<nums.length; j++){
  7. if(Math.abs(nums[i]-nums[j])==k) result+=1;
  8. // if(nums[i]-nums[j]==k) result+=1;
  9. // else if( nums[i]-nums[j]==-k) result+=1;
  10. }
  11. }
  12. return result;//3.返回result结果
  13. }
  14. }

代码2:

  1. class Solution {
  2. public int countKDifference(int[] nums, int k) {
  3. Map<Integer,Integer> map = new HashMap<>();//1.新建hashmap,定义result初始为0
  4. int result = 0;
  5. for(int i = 0;i< nums.length;i++){
  6. //2.for循环遍历
  7. if(map.containsKey(nums[i]-k)){
  8. //map中是否有key减去k,说明有和其相减的数值,如果有,map.get(nums[i]-k)为1,也就是在result基础上加1
  9. result += map.get(nums[i]-k);
  10. }
  11. if(map.containsKey(nums[i]+k)){
  12. //map中是否有key加k,说明有和其相减的数值,如果有,map.get(nums[i]+k)为1,也就是在result基础上加1
  13. result += map.get(nums[i]+k);
  14. }
  15. map.put(nums[i],map.getOrDefault(nums[i],0)+1);//map中没有元素,map将数组元素作为key放入map,默认值为0,+1是为reusult加1做铺垫
  16. }
  17. return result;//3.返回reuslt
  18. }
  19. }

解题思路基本相同

  1. class Solution {
  2. public int countKDifference(int[] nums, int k) {
  3. HashMap<Integer,Integer> hm=new HashMap<>();
  4. for(int i=0;i<nums.length;i++){
  5. hm.put(nums[i],hm.getOrDefault(nums[i],0)+1);
  6. }
  7. int count=0;
  8. for(int i=0;i<nums.length;i++){
  9. int val=nums[i]+k;
  10. if(hm.containsKey(val)){
  11. count+=hm.get(val);
  12. }
  13. }
  14. return count;
  15. }
  16. }
  17. class Solution {
  18. public static int countKDifference(int[] nums, int k) {
  19. int counter=0;
  20. HashMap<Integer,Integer> map= new HashMap<>();
  21. for (int i = 0; i <nums.length ; i++) {
  22. map.put(nums[i],map.getOrDefault(nums[i],0)+1);
  23. }
  24. for (int i = 0; i <nums.length ; i++) {
  25. if(map.containsKey(nums[i]-k))
  26. counter+=map.get(Math.abs(nums[i]-k));
  27. }
  28. return counter;
  29. }
  30. }

代码3:

  1. class Solution {
  2. public int countKDifference(int[] nums, int k) {
  3. int[] arr = new int[101];//1.定义长度为101(有1-100的数字)的arr数组,定义count初始为0
  4. int count = 0;
  5. for(int i: nums) {
  6. //2.遍历数组,将nums在i位置上相同个数+1
  7. arr[i]++;
  8. }
  9. for(int i=0; i+k<101; i++) {
  10. //3.遍历arr数组,arr[i]*arr[i+k]是在i位置上出现的重复数字个数和i+k位置上出现的重复数字个数,乘积是符合要求的个数
  11. count += arr[i]*arr[i+k];
  12. }
  13. return count;
  14. }
  15. }

补充理解:
在这里插入图片描述

  1. public int countKDifference(int[] nums, int k) {
  2. int n = nums.length;
  3. int ans =0;
  4. int [] count = new int [101];
  5. for(int i: nums)
  6. {
  7. if(i+k<=100)
  8. ans+= count[i+k];
  9. if(i-k>=1)
  10. ans+= count[i-k];
  11. count[i]++;
  12. }
  13. return ans;
  14. }

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读