leetcode 1679. Max Number of K-Sum Pairs(和为k的数字对)

该博客介绍了如何解决寻找数组中和为k的数对并删除它们的问题,提供了两种不同的解决方案:一种是使用HashMap记录每个数字出现的次数,另一种是采用双指针方法对数组排序后进行查找。这两种方法分别适用于不同情况,可以有效地找到最大操作数。
摘要由CSDN通过智能技术生成

You are given an integer array nums and an integer k.

In one operation, you can pick two numbers from the array whose sum equals k and remove them from the array.

Return the maximum number of operations you can perform on the array.

Example 1:

Input: nums = [1,2,3,4], k = 5
Output: 2
Explanation: Starting with nums = [1,2,3,4]:

  • Remove numbers 1 and 4, then nums = [2,3]
  • Remove numbers 2 and 3, then nums = []
    There are no more pairs that sum up to 5, hence a total of 2 operations.

在一个数组中找到所有的一对数字,使它们的和为k。

思路:
变相版的2 sum问题。

会想到把访问过的数字保存在HashSet里面,然后找k-另一数字,
但是,本题的数组是可以有重复数字的,所以不能用HashSet,
但是可以用HashMap,记录每个数字出现的次数,用过一次次数减1。
这种方法时间效率不咋地。

    public int maxOperations(int[] nums, int k) {
        int n = nums.length;
        HashMap<Integer, Integer> map = new HashMap<>();
        int res = 0;
        
        map.put(nums[0], 1);
        
        for(int i = 1; i< n; i++) {
            int num = k - nums[i];
            
            if(map.containsKey(num)) {
                map.put(num, map.get(num) - 1);
                if(map.get(num) == 0) map.remove(num);
                res ++;
            } else {
                if(!map.containsKey(nums[i])) map.put(nums[i], 1);
                else map.put(nums[i], map.get(nums[i]) + 1);
            }
        }
        return res;
    }

两个数字之和还可以用双指针,
和 < k 时把左指针右移,和 > k 就把右指针左移。
但是记得要先排序。

    public int maxOperations(int[] nums, int k) {
        int n = nums.length;
        int left = 0;
        int right = n - 1;
        int res = 0;
        
        Arrays.sort(nums);
        
        while(left < right) {
            int sum = nums[left] + nums[right];
            if(sum == k) {
                res ++;
                left ++;
                right --;
            } else if (sum < k) {
                left ++;
            } else {
                right --;
            }
        }
        return res;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值