【LeetCode Daily Question】——454. Addition of Four Numbers II

One [topic category]

  • hash table

Two [question difficulty]

  • medium

Three [topic number]

  • 454. Adding Four Numbers II

Four [title description]

  • Given you four integer arrays nums1, nums2, nums3 and nums4, the length of the arrays is n, please calculate how many tuples (i, j, k, l) can satisfy:
    • 0 <= i, j, k, l < n
    • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

Five [topic examples]

  • Example 1:

    • Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
    • Output: 2
    • explain:
      • The two tuples are as follows:
        1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
        2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
  • Example 2:

    • Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
    • Output: 1

Six [topic prompt]

  • n = = n u m s 1. l e n g t h n == nums1.length n==nums1.length
  • n = = n u m s 2. l e n g t h n == nums2.length n==nums2.length
  • n = = n u m s 3. l e n g t h n == nums3.length n==nums3.length
  • n = = n u m s 4. l e n g t h n == nums4.length n==nums4.length
  • 1 < = n < = 200 1 <= n <= 200 1<=n<=200
  • − 2 28 < = n u m s 1 [ i ] , n u m s 2 [ i ] , n u m s 3 [ i ] , n u m s 4 [ i ] < = 2 28 -2^{28} <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2^{28} 228<=nums1[i],nums2[i],nums3[i],nums4[i]<=228

Seven [problem-solving ideas]

  • Divide the four arrays into two groups
  • First traverse the possible combinations of all element values ​​of the two arrays in the first group, calculate their sum, and use the sum as the key of the hash table, and the val of the hash table is the number of combinations of elements with the same sum
  • Then traverse the possible combinations of all element values ​​of the two arrays of the second group, but we calculate the opposite value (just add a negative sign), if the value can be matched in the hash table, it means that the sum is 0, then accumulating the number of occurrences can be
  • Finally return the result

Eight 【Time Frequency】

  • Time complexity: O ( n 2 ) O(n^2)O ( n2) n n n is the length of the incoming array
  • Space complexity: O ( n 2 ) O(n^2)O ( n2) n n n is the length of the incoming array

Nine [code implementation]

  1. Java language version
class Solution {
    
    
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
    
    
        int n = nums1.length;
        int count = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0;i < n;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                int sum = nums1[i] + nums2[j];
                if(map.containsKey(sum)){
    
    
                    map.put(sum, map.get(sum)+ 1);
                }else{
    
    
                    map.put(sum, 1);
                }
            }
        }
        for(int i = 0;i < n;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                int oppoSum = -(nums3[i] + nums4[j]);
                if(map.containsKey(oppoSum)){
    
    
                    count += map.get(oppoSum);
                }
            }
        }
        return count;
    }
}
  1. C language version
struct hashTable
{
    
    
    int key;
    int val;
    UT_hash_handle hh;
};

int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums3, int nums3Size, int* nums4, int nums4Size)
{
    
    
    struct hashTable* map = NULL;
    int count = 0;
    for(int i = 0;i < nums1Size;i++)
    {
    
    
        for(int j = 0;j < nums1Size;j++)
        {
    
    
            int sum = nums1[i] + nums2[j];
            struct hashTable* temp;
            HASH_FIND_INT(map, &sum, temp);
            if(temp == NULL)
            {
    
    
                struct hashTable* insertVal = malloc(sizeof(struct hashTable));
                insertVal->key = sum;
                insertVal->val = 1;
                HASH_ADD_INT(map, key, insertVal);
            }
            else
            {
    
    
                temp->val++;
            }
        }
    }
    for(int i = 0;i < nums1Size;i++)
    {
    
    
        for(int j = 0;j < nums1Size;j++)
        {
    
    
            int oppo_sum = -(nums3[i] + nums4[j]);
            struct hashTable* temp;
            HASH_FIND_INT(map, &oppo_sum, temp);
            if(temp != NULL)
            {
    
    
                count += temp->val;
            }
        }
    }
    return count;
}
  1. Python language version
class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        n = len(nums1)
        count = 0
        map = {
    
    }
        for a in nums1:
            for b in nums2:
                sum = a + b
                if sum in map:
                    map[sum] = map[sum] + 1
                else:
                    map[sum] = 1
        for c in nums3:
            for d in nums4:
                oppo_sum = -(c + d)
                if oppo_sum in map:
                    count += map[oppo_sum]
        return count
  1. C++ language version
class Solution {
    
    
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
    
    
        int n = nums1.size();
        int count = 0;
        unordered_map<int, int>  map;
        for(int i = 0;i < n;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                int sum = nums1[i] + nums2[j];
                if(map.count(sum) != 0){
    
    
                    map[sum]++;
                }else{
    
    
                    map[sum] = 1;
                }
            }
        }
        for(int i = 0;i < n;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                int oppoSum = -(nums3[i] + nums4[j]);
                if(map.count(oppoSum) != 0){
    
    
                    count += map[oppoSum];
                }
            }
        }
        return count;
    }
};

Ten【Submission results】

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/132258850