Skip to content

Commit 964d3ec

Browse files
authored
Merge pull request #1300 from 0xff-dev/2956
Add solution and test-cases for problem 2956
2 parents 8c60a64 + a32c9fb commit 964d3ec

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed
1.61 MB
Loading

leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
# [2956.Find Common Elements Between Two Arrays][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+
You are given two integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively. Calculate the following values:
5+
6+
- `answer1` : the number of indices `i` such that `nums1[i]` exists in `nums2`.
7+
- `answer2` : the number of indices `i` such that `nums2[i]` exists in `nums1`.
8+
9+
Return `[answer1,answer2]`.
10+
11+
**Example 1:**
712

8-
**Example 1:**
13+
![1](./1.gif)
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums1 = [2,3,2], nums2 = [1,2]
17+
18+
Output: [2,1]
19+
20+
Explanation:
21+
```
22+
23+
**Example 2:**
24+
1325
```
26+
Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
27+
28+
Output: [3,4]
29+
30+
Explanation:
31+
32+
The elements at indices 1, 2, and 3 in nums1 exist in nums2 as well. So answer1 is 3.
1433
15-
## 题意
16-
> ...
34+
The elements at indices 0, 1, 3, and 4 in nums2 exist in nums1. So answer2 is 4.
35+
```
1736

18-
## 题解
37+
**Example 3:**
1938

20-
### 思路1
21-
> ...
22-
Find Common Elements Between Two Arrays
23-
```go
2439
```
40+
Input: nums1 = [3,4,2,3], nums2 = [1,5]
41+
42+
Output: [0,0]
2543
44+
Explanation:
45+
46+
No numbers are common between nums1 and nums2, so answer is [0,0].
47+
```
2648

2749
## 结语
2850

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums1 []int, nums2 []int) []int {
4+
m1 := make(map[int]struct{})
5+
for _, n := range nums1 {
6+
m1[n] = struct{}{}
7+
}
8+
m2 := make(map[int]struct{})
9+
for _, n := range nums2 {
10+
m2[n] = struct{}{}
11+
}
12+
ans := []int{0, 0}
13+
for _, n := range nums1 {
14+
if _, ok := m2[n]; ok {
15+
ans[0]++
16+
}
17+
}
18+
for _, n := range nums2 {
19+
if _, ok := m1[n]; ok {
20+
ans[1]++
21+
}
22+
}
23+
return ans
524
}

leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums1, nums2 []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 3, 2}, []int{1, 2}, []int{2, 1}},
17+
{"TestCase2", []int{4, 3, 2, 3, 1}, []int{2, 2, 5, 2, 3, 6}, []int{3, 4}},
18+
{"TestCase3", []int{3, 4, 2, 3}, []int{1, 5}, []int{0, 0}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums1, c.nums2)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums1, c.nums2)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)