Skip to content

Commit

Permalink
Merge pull request #922 from 0xff-dev/350
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 350
  • Loading branch information
6boris authored Sep 9, 2024
2 parents b4ae9e9 + de68443 commit be90169
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
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() {
}

0 comments on commit be90169

Please sign in to comment.