[Swift]LeetCode454. 四数相加 II | 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


给定四个包含整数的数组列表 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

264ms
 1 class Solution {
 2     func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
 3         var map: [Int: Int] = [:]
 4         for i in 0..<A.count {
 5             for j in 0..<B.count {
 6                 let sum = A[i] + B[j]
 7                 map[sum, default: 0] += 1
 8             }
 9         }
10
11         var count = 0
12         for i in 0..<C.count {
13             for j in 0..<D.count {
14                 let sum = C[i]+D[j]
15                 if let negativeOfSum = map[-sum] {
16                     count += negativeOfSum
17                 }
18             }
19         }
20
21         return count
22     }
23 }


312ms

 1 class Solution {
 2     func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
 3         var map: [Int: Int] = [:]
 4         for i in A.indices {
 5             for j in B.indices {
 6                 map[A[i] + B[j], default: 0] += 1
 7             }
 8         }
 9
10         var result = 0
11         for i in C.indices {
12             for j in D.indices {
13                 result += map[-C[i]-D[j], default:0]
14             }
15         }
16
17         return result
18     }
19 }


436ms

 1  class Solution {
 2         func Max(_ A: ([Int], Int), _ B: ([Int], Int)) -> ([Int], Int) {
 3             if A.0.count > B.0.count {
 4                 return A
 5             }else{
 6                 return B
 7             }
 8         }
 9         func Min(_ A: ([Int], Int), _ B: ([Int], Int)) -> ([Int], Int) {
10             if A.0.count < B.0.count {
11                 return A
12             }else{
13                 return B
14             }
15         }
16         func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
17             var nums = [A, B, C, D]
18             let A1 = Max(Max((nums[0],0), (nums[1],1)), Max((nums[2],2), (nums[3],3)))
19             nums.remove(at: A1.1)
20             let B1 = Min(Min((nums[0],0), (nums[1],1)), (nums[2], 2))
21             nums.remove(at: B1.1)
22             let C1 = nums[0]
23             let D1 = nums[1]
24
25             var dict = [Int:Int]()
26             for a in A1.0 {
27                 for b in B1.0 {
28                     if let n = dict[a + b]{
29                         dict[a + b] = n + 1
30                     }else{
31                         dict[a + b] = 1
32                     }
33                 }
34             }
35             var count = 0
36             for c in C1 {
37                 for d in D1 {
38                     if let n = dict[0 - c - d] {
39                         count += n
40                     }
41                 }
42             }
43             return count
44         }
45     }


540ms

 1 class Solution {
 2     func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
 3         var res:Int = 0
 4         var n:Int = A.count
 5         var m1:[Int:Int] = [Int:Int]()
 6         var m2:[Int:Int] = [Int:Int]()
 7         for i in 0..<n
 8         {
 9             for j in 0..<n
10             {
11                 if m1[A[i] + B[j]] == nil
12                 {
13                     m1[A[i] + B[j]] = 1
14                 }
15                 else
16                 {
17                     m1[A[i] + B[j]]! += 1
18                 }
19                 if m2[C[i] + D[j]] == nil
20                 {
21                     m2[C[i] + D[j]] = 1
22                 }
23                 else
24                 {
25                     m2[C[i] + D[j]]! += 1
26                 }
27             }
28         }
29         for (key,val) in m1
30         {
31             if m2[-key] != nil
32             {
33                 res += val * m2[-key]!
34             }
35         }
36         return res
37     }
38 }


548ms

 1 class Solution {
 2     func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
 3         var result = 0
 4         var map: [Int: Int] = [:]
 5         for a in A {
 6             for b in B {
 7                  let sum = a + b
 8                 if let count = map[sum] {
 9                     map[sum] = count + 1
10                 } else {
11                     map[sum] = 1
12                 }
13             }
14         }
15
16         for c in C {
17             for d in D {
18                 let sum = -(c + d)
19                 if let count = map[sum] {
20                     result += count
21                 }
22             }
23         }
24
25         return result
26     }
27 }


880ms

 1 class Solution {
 2     func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
 3
 4         var res = 0
 5
 6         var CDRecord = [Int : Int]()
 7
 8         for c in C {
 9             for d in D {
10                if let r = CDRecord[c + d] {
11                    CDRecord[c + d] = r + 1
12                } else {
13                     CDRecord[c + d] = 1
14                }
15             }
16         }
17
18         for a in A {
19             for b in B {
20                 let complete = 0 - (a + b)
21
22                 if let value = CDRecord[complete] {
23                     res += value
24                 }
25             }
26         }
27         return res
28     }
29 }

原文地址:https://www.cnblogs.com/strengthen/p/10343175.html

时间: 2024-11-08 20:16:44

[Swift]LeetCode454. 四数相加 II | 4Sum II的相关文章

Leetcode 454.四数相加II

四数相加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

leetcode 454. 四数相加 II java

题目: 给定四个包含整数的数组列表 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 解释:两个元组如

[Swift]LeetCode18. 四数之和 | 4Sum

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain dupli

LeetCode 18. 四数之和(4Sum)

题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]

[LeetCode] 454. 4Sum II 四数之和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 r

LeetCode 445. 两数相加 II(Add Two Numbers II)

445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. LeetCode445. Add Two Numbers II中等 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 ->

445. 两数相加 II

给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) 输出: 7 -> 8 -> 0 -> 7 1 import java.util.Stack; 2 3 public class AddTwoNumbersII { 4 public ListNode addTwoNumbers(ListNode l1, L

LeetCode 445 两数相加 II

链接:https://leetcode-cn.com/problems/add-two-numbers-ii 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)输出: 7 -&g

LeetCode 454. 4Sum II

454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18801 Difficulty: Medium Contributors: Samuri Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[