【LeetCode】454. 四数相加 II

文章讲述了如何使用Python实现LeetCode上的一个题目,通过将四重循环简化为两个二层循环,利用字典统计两数之和及其出现次数,有效降低时间复杂度,计算满足nums1[i]+nums2[j]+nums3[k]+nums4[l]==0的元组数量。

目录

题目

思路

代码


题目

题目链接:. - 力扣(LeetCode)

给你四个整数数组 nums1nums2nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

示例 1:

输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
输出:2
解释:
两个元组如下:
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

示例 2:

输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
输出:1

  提示:

  • n == nums1.length
  • n == nums2.length
  • n == nums3.length
  • n == nums4.length
  • 1 <= n <= 200
  • -228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228

思路

为了降低时间复杂度,将四重for循环拆分为两个二层for循环

1.将nums1、nums2与nums3、nums4拆分为两组

2.遍历nums1与nums2,用字典sum1记录两数之和以及和出现的次数,key为两数之和,value为出现次数,其他语言可以用map存储

3.用count记录四数和为0的次数,count=0

4.遍历nums3与nums4,对两数求和的同时,在字典sum1中寻找是否有与之相加和为0的key,如有,count加上对应value的值

代码

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        count = 0
        sum1 = dict()
        for i in nums1:
            for j in nums2:
                sum1[i+j] = sum1.get(i+j,0) + 1
        
        for i in nums3:
            for j in nums4:
                target = 0 - i - j
                if target in sum1:
                    count += sum1[target]
        return count

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 : &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 = &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、付费专栏及课程。

余额充值