Skip to content

Commit de68443

Browse files
committed
Add solution and test-cases for problem 350
1 parent 9f83439 commit de68443

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

leetcode/301-400/0350.Intersection-of-Two-Arrays-II/README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
# [350.Intersection of Two Arrays II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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**.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
10+
Output: [2,2]
1311
```
1412

15-
## 题意
16-
> ...
17-
18-
## 题解
13+
**Example 2:**
1914

20-
### 思路1
21-
> ...
22-
Intersection of Two Arrays II
23-
```go
2415
```
25-
16+
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
17+
Output: [4,9]
18+
Explanation: [9,4] is also accepted.
19+
```
2620

2721
## 结语
2822

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums1 []int, nums2 []int) []int {
4+
in := [1001]int{}
5+
for _, n := range nums1 {
6+
in[n]++
7+
}
8+
ans := make([]int, 0)
9+
for _, n := range nums2 {
10+
if in[n] > 0 {
11+
ans = append(ans, n)
12+
in[n]--
13+
}
14+
}
15+
return ans
516
}

leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n1, n2 []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 2, 1}, []int{2, 2}, []int{2, 2}},
17+
{"TestCase2", []int{4, 9, 5}, []int{9, 4, 9, 8, 4}, []int{9, 4}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n1, c.n2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.n1, c.n2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)