数组练习题11--找出和为指定值的下标对

给你两个整数数组nums1和nums2,请你支持实现一个下述两类查询的数据结构:

  • 累加,将一个正整数加到nums2中的指定下标对应的元素上。
  • 计数,统计满足nums[i]+nums[j]等于指定值的下标对(i,j)数目(0<=i<nums1.length)且 0 <= j < nums2.length)。

实现FindSumPairs 类:

  • FindSumPairs(int[] nums1, int[] nums2) 使用整数数组 nums1 和 nums2 初始化 FindSumPairs 对象。
  • void add(int index, int val) 将 val 加到 nums2[index] 上,即,执行 nums2[index] += val 。
  • int count(int tot) 返回满足 nums1[i] + nums2[j] == tot 的下标对 (i, j) 数目。

示例1:

输入
["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
输出:
[null, 8, null, 2, 1, null, null, 11]

解释:
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
findSumPairs.count(7);  // 返回 8 ; 下标对 (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) 满足 2 + 5 = 7 ,下标对 (5,1), (5,5) 满足 3 + 4 = 7
findSumPairs.add(3, 2); // 此时 nums2 = [1,4,5,4,5,4]
findSumPairs.count(8);  // 返回 2 ;下标对 (5,2), (5,4) 满足 3 + 5 = 8
findSumPairs.count(4);  // 返回 1 ;下标对 (5,0) 满足 3 + 1 = 4
findSumPairs.add(0, 1); // 此时 nums2 = [2,4,5,4,5,4]
findSumPairs.add(1, 1); // 此时 nums2 = [2,5,5,4,5,4]
findSumPairs.count(7);  // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) 满足 2 + 5 = 7 ,下标对 (5,3), (5,5) 满足 3 + 4 = 7

思路,一开始我用了一个笨比方法,导致超时,最后一个案例就是过不去,压根没想到数据结构上去,虽然学过但没想起用,结果看答案才明白:先看看自己的笨比办法,觉得好笑!老是想着排序,变成两数之和去做,属实蠢!

class FindSumPairs {
    private int[] nums1;
    private int[] nums2;
    public FindSumPairs(int[] nums1, int[] nums2) {
        this.nums1=nums1;
        this.nums2=nums2;
    }
    
    public void add(int index, int val) {
        nums2[index]+=val;
    }
    
    public int count(int tot) {
        int sum=0;
        int [] num1=Arrays.copyOfRange(nums1,0,nums1.length);
        int [] num2=Arrays.copyOfRange(nums2,0,nums2.length);
        Arrays.sort(num1);
        Arrays.sort(num2);
        int i=0,j=num2.length-1;
        if(num1[0]+num2[0]>tot||num1[num1.length-1]+num2[num2.length-1]<tot)
            return 0;
        while(i<num1.length&&j>=0){
            int count=0;
            if(num1[i]+num2[j]<tot){
                i++;
            }
            else if(num1[i]+num2[j]>tot){
                j--;
            }
            else{
                count++;
                i++;
                while(i<num1.length&&num1[i]==num1[i-1]){
                    count++;
                    i++;
                }
                j--;
                sum=sum+count;
                while (j>=0&&num2[j]==num2[j+1]){
                    sum=sum+count;
                    j--;
                }
            }

        }
        return sum;
    }
}

/**
 * Your FindSumPairs object will be instantiated and called as such:
 * FindSumPairs obj = new FindSumPairs(nums1, nums2);
 * obj.add(index,val);
 * int param_2 = obj.count(tot);
 */

正确思路:利用哈希集合来完成。要学以致用,被持续教育中。。。

方法1:哈希集合

class FindSumPairs {
    private int[] nums1;
    private int[]nums2;
    HashMap<Integer,Integer> record=new HashMap<>();
    public FindSumPairs(int[] nums1, int[] nums2) {
        this.nums1=nums1;
        this.nums2=nums2;
        for(int num:nums2){
            record.put(num,record.getOrDefault(num,0)+1);
        }
    }
    
    public void add(int index, int val) {
        record.put(nums2[index],record.get(nums2[index])-1);
        nums2[index]+=val;
        record.put(nums2[index],record.getOrDefault(nums2[index],0)+1);
    }
    
    public int count(int tot) {
        int sum=0;
        for(int i=0;i<nums1.length;i++){
            sum+=record.getOrDefault(tot-nums1[i],0);
        }
        return sum;
    }
}

/**
 * Your FindSumPairs object will be instantiated and called as such:
 * FindSumPairs obj = new FindSumPairs(nums1, nums2);
 * obj.add(index,val);
 * int param_2 = obj.count(tot);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值