diff --git a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/1.gif b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/1.gif new file mode 100644 index 000000000..f47c859fc Binary files /dev/null and b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/1.gif differ diff --git a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/README.md b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/README.md index b9ac28e49..45842cbd3 100755 --- a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/README.md +++ b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/README.md @@ -1,28 +1,50 @@ # [2956.Find Common Elements Between Two Arrays][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 +You are given two integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively. Calculate the following values: + +- `answer1` : the number of indices `i` such that `nums1[i]` exists in `nums2`. +- `answer2` : the number of indices `i` such that `nums2[i]` exists in `nums1`. + +Return `[answer1,answer2]`. + +**Example 1:** -**Example 1:** +![1](./1.gif) ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums1 = [2,3,2], nums2 = [1,2] + +Output: [2,1] + +Explanation: +``` + +**Example 2:** + ``` +Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] + +Output: [3,4] + +Explanation: + +The elements at indices 1, 2, and 3 in nums1 exist in nums2 as well. So answer1 is 3. -## 题意 -> ... +The elements at indices 0, 1, 3, and 4 in nums2 exist in nums1. So answer2 is 4. +``` -## 题解 +**Example 3:** -### 思路1 -> ... -Find Common Elements Between Two Arrays -```go ``` +Input: nums1 = [3,4,2,3], nums2 = [1,5] + +Output: [0,0] +Explanation: + +No numbers are common between nums1 and nums2, so answer is [0,0]. +``` ## 结语 diff --git a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution.go b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution.go index d115ccf5e..739661e7f 100644 --- a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution.go +++ b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums1 []int, nums2 []int) []int { + m1 := make(map[int]struct{}) + for _, n := range nums1 { + m1[n] = struct{}{} + } + m2 := make(map[int]struct{}) + for _, n := range nums2 { + m2[n] = struct{}{} + } + ans := []int{0, 0} + for _, n := range nums1 { + if _, ok := m2[n]; ok { + ans[0]++ + } + } + for _, n := range nums2 { + if _, ok := m1[n]; ok { + ans[1]++ + } + } + return ans } diff --git a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution_test.go b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution_test.go index 14ff50eb4..2dca5eebf 100644 --- a/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution_test.go +++ b/leetcode/2901-3000/2956.Find-Common-Elements-Between-Two-Arrays/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums1, nums2 []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 3, 2}, []int{1, 2}, []int{2, 1}}, + {"TestCase2", []int{4, 3, 2, 3, 1}, []int{2, 2, 5, 2, 3, 6}, []int{3, 4}}, + {"TestCase3", []int{3, 4, 2, 3}, []int{1, 5}, []int{0, 0}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums1, c.nums2) 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.nums1, c.nums2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }