From de684439a95a23a683958760ac9be1c3846578e8 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 2 Jul 2024 08:44:34 +0800 Subject: [PATCH] Add solution and test-cases for problem 350 --- .../README.md | 22 +++++++------------ .../Solution.go | 15 +++++++++++-- .../Solution_test.go | 19 ++++++++-------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/README.md b/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/README.md index 6b910a36a..637dc50b9 100644 --- a/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/README.md +++ b/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution.go b/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution.go index d115ccf5e..b190eeda1 100644 --- a/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution.go +++ b/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution.go @@ -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 } diff --git a/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution_test.go b/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution_test.go index 14ff50eb4..1b3447bf5 100644 --- a/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution_test.go +++ b/leetcode/301-400/0350.Intersection-of-Two-Arrays-II/Solution_test.go @@ -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() { }