day6 | 454.四数相加II 、383. 赎金信 、15. 三数之和 、18. 四数之和 、总结

454.四数相加II

在这里插入图片描述
判断 nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

思路同两数之和:
可以 选择 nums[i] + nums[j] 为 key,次数 count 为 value;
可以 判断 0 - (nums[k] + nums[l]) 是否 为 key, 结果 加上 value的值。

完整代码:

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        // a + b + c + d = 0
        // 遍历1,2: map记录 k:(a+b) v:(a+b)的次数
        // 遍历3,4:寻找k = 0 - (a+b)的 v, count 加上 v 

        HashMap<Integer, Integer> map = new HashMap<>();
        
        for (int n1: nums1) {
            for (int n2: nums2) {
                map.put(n1 + n2, map.getOrDefault(n1 + n2, 0) + 1);
            }
        }

        int count = 0;
        for (int n3: nums3) {
            for (int n4: nums4) {
                int temp = n3 + n4;
                if (map.containsKey(0 - temp)) {
                    count += map.get(0 - temp);
                }
            }
        }
        return count;
    }
}

383. 赎金信

使用word数组记录 magazine 中每个字符数量,遍历 ransomNote 的字符数量。
如果记录word中有 < 0 时,即无法构成 ransomNote。

15. 三数之和

[a, b, c] 同四数之和一样的解法,使用双指针指向b和c,遍历a。
同时注意去重的操作。
a:

if (i > 0 && nums[i-1] == nums[i]) continue;

b和c:

while (l < r && nums[l] == nums[l+1]) l++;
while (l < r && nums[r] == nums[r-1]) r--;
l++;
r--;

18. 四数之和

注意去重和优化
a + b + c + d

当 a>0 且 > target 时,后面均不满足了。

if (nums[i] > 0 && nums[i] > target) break; // 优化

完整代码

package code.D.hash;

import java.util.*;

public class LC18_fourSum {

    public static void main(String[] args) {
        int[] nums = new int[]{1,0,-1,0,-2,2};
        List<List<Integer>> res = fourSum(nums, 0);
        System.out.println(res);
    }
    public static List<List<Integer>> fourSum(int[] nums, int target) { 
        
        List<List<Integer>> res = new ArrayList<>();

        int n = nums.length;
        if (n < 4) return res;

        Arrays.sort(nums);

        for (int i = 0; i < n-3; i++) {
            if (nums[i] > 0 && nums[i] > target) break; // 优化

            if (i > 0 && nums[i-1] == nums[i]) continue;

            for (int j = i+1; j < n-2; j++) {
                if (j > i+1 && nums[j-1] == nums[j]) continue;

                int l = j + 1, r = n-1;

                long lsum = (long) nums[i] + nums[j] + nums[l] + nums[l+1];
                long rsum = (long) nums[i] + nums[j] + nums[r] + nums[r-1];
                if (lsum > target) break;
                if (rsum < target) continue;

                while (l < r) {
                    long sum = (long) nums[i] + nums[j] + nums[l] + nums[r];

                    if (sum < target) l++;
                    else if (sum > target) r--;
                    else {
                        res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
                        while (l < r && nums[l] == nums[l+1]) l++;
                        while (l < r && nums[r] == nums[r-1]) r--;

                        l++;
                        r--;
                    }
                }
            }
        }
        return res;
    }
}

代码很长,不细心很容易就写错了。。还不容易发现。。

总结

四数之和返回元组的数量,可以使用map记录count
但是四数之和|| 和 三数之和 返回的是元组 (i, j, k, l),需要记录索引,所以使用双指针法更适合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值