mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
添加 problem 454
This commit is contained in:
18
Algorithms/0454. 4Sum II/454. 4Sum II.go
Normal file
18
Algorithms/0454. 4Sum II/454. 4Sum II.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func fourSumCount(A []int, B []int, C []int, D []int) int {
|
||||||
|
m := make(map[int]int, len(A)*len(B))
|
||||||
|
for _, a := range A {
|
||||||
|
for _, b := range B {
|
||||||
|
m[a+b]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret := 0
|
||||||
|
for _, c := range C {
|
||||||
|
for _, d := range D {
|
||||||
|
ret += m[0-c-d]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
45
Algorithms/0454. 4Sum II/454. 4Sum II_test.go
Normal file
45
Algorithms/0454. 4Sum II/454. 4Sum II_test.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question454 struct {
|
||||||
|
para454
|
||||||
|
ans454
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para454 struct {
|
||||||
|
a []int
|
||||||
|
b []int
|
||||||
|
c []int
|
||||||
|
d []int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans454 struct {
|
||||||
|
one int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem454(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question454{
|
||||||
|
|
||||||
|
question454{
|
||||||
|
para454{[]int{1, 2}, []int{-2, -1}, []int{-1, 2}, []int{0, 2}},
|
||||||
|
ans454{2},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 454------------------------\n")
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans454, q.para454
|
||||||
|
fmt.Printf("【input】:%v 【output】:%v\n", p, fourSumCount(p.a, p.b, p.c, p.d))
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n\n")
|
||||||
|
}
|
37
Algorithms/0454. 4Sum II/README.md
Normal file
37
Algorithms/0454. 4Sum II/README.md
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# [454. 4Sum II](https://leetcode.com/problems/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 1:
|
||||||
|
|
||||||
|
```c
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 题目大意
|
||||||
|
|
||||||
|
给出 4 个数组,计算这些数组中存在几对 i,j,k,l 可以使得 A[i] + B[j] + C[k] + D[l] = 0 。
|
||||||
|
|
||||||
|
## 解题思路
|
||||||
|
|
||||||
|
这道题的数据量不大,0 ≤ N ≤ 500,但是如果使用暴力解法,四层循环,会超时。这道题的思路和第 1 题思路也类似,先可以将 2 个数组中的组合都存入 map 中。之后将剩下的 2 个数组进行 for 循环,找出和为 0 的组合。这样时间复杂度是 O(n^2)。当然也可以讲剩下的 2 个数组的组合也存入 map 中,不过最后在 2 个 map 中查找结果也是 O(n^2) 的时间复杂度。
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user