leetcode 454. 四数相加 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 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。
示例:

输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

输出:
2

解释:
两个元组如下:
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

此题是18题的延申,四个数组各取一个数使其和为0。我们将A和B的两两之和都求出来,在HashMap中建立两数之和和出现次数的映射,再遍历C和D的任意两个数之和,看哈希表存不存在这两个数的相反数即可。
C++

class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        int res = 0;
        unordered_map<int, int> m;
        for (int i = 0; i < A.size(); ++i){
            for (int j = 0; j < B.size(); ++j){
                ++m[A[i]+B[j]];
            }
        }
        for (int i = 0; i < C.size(); ++i){
            for (int j = 0; j < D.size(); ++j){
                int target = 0 - (C[i] + D[j]);
                res += m[target];
            }
        }
        return res;
    }
};

改成用两个HashMap分别记录AB和CD的两数之和和次数,遍历其中一个HashMap,并在另外一个中找到相反数出现的次数。

class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        int res = 0, n = A.size();
        unordered_map<int, int> m1,m2;
        for (int i = 0; i < n; ++i){
            for (int j = 0; j < n; ++j){
                ++m1[A[i] + B[j]];
                ++m2[C[i] + D[j]];
            }
        }
        for (auto a : m1) res += a.second * m2[-a.first];
        return res;
    }
};

Python

class Solution(object):
    def fourSumCount(self, A, B, C, D):
        """
        :type A: List[int]
        :type B: List[int]
        :type C: List[int]
        :type D: List[int]
        :rtype: int
        """
        res = 0
        ab_map = dict()
        for a in A:
            for b in B:
                ab_map[a+b] = ab_map.get(a+b, 0) + 1
        
        for c in C:
            for d in D:
                s = -(c+d)
                if s in ab_map:
                    res += ab_map[s]
        return res
LeetCode 题目2《两数相加》是一道经典的算法题目,主要涉及基本的数组操作和整数计算,目标是给定两个非负整数,通过栈的数据结构来求它们的和。以下是使用 C 语言的一个简单解决方案: ```c #include <stdio.h> // 定义一个结构体存放链表节点 typedef struct ListNode { int val; struct ListNode *next; } ListNode; // 将输入的十进制数字转换成链表表示形式 ListNode* createListNode(int num) { ListNode* head = (num == 0) ? NULL : &amp;head; while (num > 0) { head->val = num % 10; num /= 10; head = head->next; if (head == NULL) { head = (ListNode*)malloc(sizeof(ListNode)); head->next = NULL; } } return head; } // 将链表表示的两个数相加并返回结果链表 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode dummy(0), *curr = &amp;dummy; int carry = 0; while (l1 != NULL || l2 != NULL) { int a = (l1 != NULL) ? l1->val : 0; int b = (l2 != NULL) ? l2->val : 0; int sum = a + b + carry; carry = sum / 10; // 计算进位 curr->next = createListNode(sum % 10); // 创建新的结点存储当前位的值 curr = curr->next; if (l1 != NULL) { l1 = l1->next; } if (l2 != NULL) { l2 = l2->next; } } // 如果最后还有进位,则在链表末尾添加一个结点表示 if (carry > 0) { curr->next = createListNode(carry); } return dummy.next; } int main() { ListNode* l1 = createListNode(2); l1->next = createListNode(4); l1->next->next = createListNode(3); ListNode* l2 = createListNode(5); l2->next = createListNode(6); l2->next->next = createListNode(4); ListNode* result = addTwoNumbers(l1, l2); while (result != NULL) { printf("%d", result->val); result = result->next; } return 0; } ``` 这个程序首先将两个输入的整数转换成链表的形式,然后逐位相加,每一步都处理了进位的情况,并保持链表结构。最终得到的结果也是一个链表。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值