• [LeetCode] 454. 4Sum II


    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

    To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

    Example:

    Input:
    A = [ 1, 2]
    B = [-2,-1]
    C = [-1, 2]
    D = [ 0, 2]
    
    Output:
    2
    
    Explanation:
    The two tuples are:
    1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
    2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

    四数相加II。

    题意是给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1。

    思路是hashmap。将A和B分为一组,C和D分为一组。因为求的是A + B + C + D = 0,所以能得出结论A + B = -C - D。按照这个思路,求出A + B的和的所有可能,存在hashmap里面,key是A + B的和,value是出现次数。所以当算完A + B的所有可能之后,再遍历C和D,找-C - D是否在hashmap里出现过,若出现过,则直接将出现次数加到结果中。

    时间O(n^2)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
     3         HashMap<Integer, Integer> map = new HashMap<>();
     4         int res = 0;
     5         for (int a : A) {
     6             for (int b : B) {
     7                 int sum = a + b;
     8                 map.put(sum, map.getOrDefault(sum, 0) + 1);
     9             }
    10         }
    11         for (int c : C) {
    12             for (int d : D) {
    13                 int sum = -c - d;
    14                 res += map.getOrDefault(sum, 0);
    15             }
    16         }
    17         return res;
    18     }
    19 }

    JavaScript实现

     1 /**
     2  * @param {number[]} A
     3  * @param {number[]} B
     4  * @param {number[]} C
     5  * @param {number[]} D
     6  * @return {number}
     7  */
     8 var fourSumCount = function (A, B, C, D) {
     9     let map = new Map(),
    10         ans = 0,
    11         n = A.length;
    12 
    13     for (let i = 0; i < n; i++) {
    14         let a = A[i];
    15         for (let j = 0; j < n; j++) {
    16             let b = B[j];
    17             if (!map.has(a + b)) {
    18                 map.set(a + b, 1);
    19             } else {
    20                 map.set(a + b, map.get(a + b) + 1);
    21             }
    22         }
    23     }
    24 
    25     for (let k = 0; k < n; k++) {
    26         let c = C[k];
    27         for (let l = 0; l < n; l++) {
    28             let d = D[l];
    29             let sum = -(c + d);
    30             if (map.has(sum)) {
    31                 ans += map.get(sum);
    32             }
    33         }
    34     }
    35     return ans;
    36 };

    LeetCode 题目总结

  • 相关阅读:
    Proguard打包混淆报错:can't find superclass or interface
    proguard returned with error code 1.异常的解决方法
    android 混淆配置
    解决android混淆编译出现Proguard returned with error code 1和文件名、目录名或卷标语法不正确错误
    Eclipse提示No java virtual machine
    [mysql]数据库查询实例
    [算法]高效求素数
    [笔试]程序员面试宝典
    [linux]进程间通信IPC
    [linux]信号的捕获和处理
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12776673.html
Copyright © 2020-2023  润新知