Skip to content

Commit

Permalink
Merge pull request #647 from 0xff-dev/454
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 454
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 4db3ed3 + 3ebd495 commit 69447ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
28 changes: 14 additions & 14 deletions leetcode/401-500/0454.4Sum-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# [454.4Sum II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples (`i, j, k, l`) such that:

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

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
Output: 2
Explanation:
The two tuples are:
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:**

### 思路1
> ...
4Sum II
```go
```

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

## 结语

Expand Down
20 changes: 18 additions & 2 deletions leetcode/401-500/0454.4Sum-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
l := len(nums1)
cache := make(map[int]int)
for i := 0; i < l; i++ {
for j := 0; j < l; j++ {
cache[nums1[i]+nums2[j]]++
}
}
ans := 0
for i := 0; i < l; i++ {
for j := 0; j < l; j++ {
target := 0 - nums3[i] - nums4[j]
if v, ok := cache[target]; ok {
ans += v
}
}
}
return ans
}
21 changes: 10 additions & 11 deletions leetcode/401-500/0454.4Sum-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n1, n2, n3, n4 []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2}, []int{-2, -1}, []int{-1, 2}, []int{0, 2}, 2},
{"TestCase2", []int{0}, []int{0}, []int{0}, []int{0}, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n1, c.n2, c.n3, c.n4)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
c.expect, got, c.n1, c.n2, c.n3, c.n4)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 69447ea

Please sign in to comment.