Skip to content

Add solution and test-cases for problem 350 #922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions leetcode/301-400/0350.Intersection-of-Two-Arrays-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
# [350.Intersection of Two Arrays 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 two integer arrays `nums1` and `nums2`, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Intersection of Two Arrays II
```go
```

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Explanation: [9,4] is also accepted.
```

## 结语

Expand Down
15 changes: 13 additions & 2 deletions leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums1 []int, nums2 []int) []int {
in := [1001]int{}
for _, n := range nums1 {
in[n]++
}
ans := make([]int, 0)
for _, n := range nums2 {
if in[n] > 0 {
ans = append(ans, n)
in[n]--
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n1, n2 []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 2, 1}, []int{2, 2}, []int{2, 2}},
{"TestCase2", []int{4, 9, 5}, []int{9, 4, 9, 8, 4}, []int{9, 4}},
}

// 开始测试
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)
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",
c.expect, got, c.n1, c.n2)
}
})
}
}

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

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