From 9cfbd98e5402f11b215dffb1f065babb74715476 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 28 Jun 2024 06:29:14 +0000 Subject: [PATCH] feat: update solutions to lc problems: No.0349,0350 * No.0349.Intersection of Two Arrays * No.0350.Intersection of Two Arrays II --- .../Solution.cs | 17 +- .../solution.cs | 10 - .../README.md | 185 +++++++++--------- .../README_EN.md | 185 +++++++++--------- .../Solution.cpp | 17 +- .../Solution.cs | 28 +-- .../Solution.go | 19 +- .../Solution.java | 21 +- .../Solution.js | 17 +- .../Solution.php | 25 ++- .../Solution.py | 14 +- .../Solution.rs | 22 ++- .../Solution.ts | 18 +- 13 files changed, 278 insertions(+), 300 deletions(-) delete mode 100644 solution/0300-0399/0349.Intersection of Two Arrays/solution.cs diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cs b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cs index f62d6b57dbf96..abd947b382d5d 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cs +++ b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cs @@ -1,13 +1,10 @@ public class Solution { public int[] Intersection(int[] nums1, int[] nums2) { - List result = new List(); - HashSet arr1 = new(nums1); - HashSet arr2 = new(nums2); - foreach (int x in arr1) { - if (arr2.Contains(x)) { - result.Add(x); - } - } - return result.ToArray(); + HashSet s1 = new HashSet(nums1); + HashSet s2 = new HashSet(nums2); + s1.IntersectWith(s2); + int[] ans = new int[s1.Count]; + s1.CopyTo(ans); + return ans; } -} +} \ No newline at end of file diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs b/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs deleted file mode 100644 index abd947b382d5d..0000000000000 --- a/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs +++ /dev/null @@ -1,10 +0,0 @@ -public class Solution { - public int[] Intersection(int[] nums1, int[] nums2) { - HashSet s1 = new HashSet(nums1); - HashSet s2 = new HashSet(nums2); - s1.IntersectWith(s2); - int[] ans = new int[s1.Count]; - s1.CopyTo(ans); - return ans; - } -} \ No newline at end of file diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md index 9c98b16525bb8..4e7a31d0fa6ea 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md @@ -62,7 +62,13 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们可以用一个哈希表 $\text{cnt}$ 统计数组 $\text{nums1}$ 中每个元素出现的次数,然后遍历数组 $\text{nums2}$,如果元素 $x$ 在 $\text{cnt}$ 中,并且 $x$ 的出现次数大于 $0$,那么将 $x$ 加入答案,然后将 $x$ 的出现次数减一。 + +遍历结束后,返回答案数组即可。 + +时间复杂度 $O(m + n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $\text{nums1}$ 和 $\text{nums2}$ 的长度。 @@ -71,13 +77,13 @@ tags: ```python class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: - counter = Counter(nums1) - res = [] - for num in nums2: - if counter[num] > 0: - res.append(num) - counter[num] -= 1 - return res + cnt = Counter(nums1) + ans = [] + for x in nums2: + if cnt[x]: + ans.append(x) + cnt[x] -= 1 + return ans ``` #### Java @@ -85,22 +91,17 @@ class Solution: ```java class Solution { public int[] intersect(int[] nums1, int[] nums2) { - Map counter = new HashMap<>(); - for (int num : nums1) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + int[] cnt = new int[1001]; + for (int x : nums1) { + ++cnt[x]; } - List t = new ArrayList<>(); - for (int num : nums2) { - if (counter.getOrDefault(num, 0) > 0) { - t.add(num); - counter.put(num, counter.get(num) - 1); + List ans = new ArrayList<>(); + for (int x : nums2) { + if (cnt[x]-- > 0) { + ans.add(x); } } - int[] res = new int[t.size()]; - for (int i = 0; i < res.length; ++i) { - res[i] = t.get(i); - } - return res; + return ans.stream().mapToInt(Integer::intValue).toArray(); } } ``` @@ -111,16 +112,17 @@ class Solution { class Solution { public: vector intersect(vector& nums1, vector& nums2) { - unordered_map counter; - for (int num : nums1) ++counter[num]; - vector res; - for (int num : nums2) { - if (counter[num] > 0) { - --counter[num]; - res.push_back(num); + unordered_map cnt; + for (int x : nums1) { + ++cnt[x]; + } + vector ans; + for (int x : nums2) { + if (cnt[x]-- > 0) { + ans.push_back(x); } } - return res; + return ans; } }; ``` @@ -128,19 +130,18 @@ public: #### Go ```go -func intersect(nums1 []int, nums2 []int) []int { - counter := make(map[int]int) - for _, num := range nums1 { - counter[num]++ +func intersect(nums1 []int, nums2 []int) (ans []int) { + cnt := map[int]int{} + for _, x := range nums1 { + cnt[x]++ } - var res []int - for _, num := range nums2 { - if counter[num] > 0 { - counter[num]-- - res = append(res, num) + for _, x := range nums2 { + if cnt[x] > 0 { + ans = append(ans, x) + cnt[x]-- } } - return res + return } ``` @@ -148,19 +149,17 @@ func intersect(nums1 []int, nums2 []int) []int { ```ts function intersect(nums1: number[], nums2: number[]): number[] { - const map = new Map(); - for (const num of nums1) { - map.set(num, (map.get(num) ?? 0) + 1); + const cnt: Record = {}; + for (const x of nums1) { + cnt[x] = (cnt[x] || 0) + 1; } - - const res = []; - for (const num of nums2) { - if (map.has(num) && map.get(num) !== 0) { - res.push(num); - map.set(num, map.get(num) - 1); + const ans: number[] = []; + for (const x of nums2) { + if (cnt[x]-- > 0) { + ans.push(x); } } - return res; + return ans; } ``` @@ -168,21 +167,23 @@ function intersect(nums1: number[], nums2: number[]): number[] { ```rust use std::collections::HashMap; + impl Solution { pub fn intersect(nums1: Vec, nums2: Vec) -> Vec { - let mut map = HashMap::new(); - for num in nums1.iter() { - *map.entry(num).or_insert(0) += 1; + let mut cnt = HashMap::new(); + for &x in &nums1 { + *cnt.entry(x).or_insert(0) += 1; } - - let mut res = vec![]; - for num in nums2.iter() { - if map.contains_key(num) && map.get(num).unwrap() != &0 { - map.insert(num, map.get(&num).unwrap() - 1); - res.push(*num); + let mut ans = Vec::new(); + for &x in &nums2 { + if let Some(count) = cnt.get_mut(&x) { + if *count > 0 { + ans.push(x); + *count -= 1; + } } } - res + ans } } ``` @@ -196,18 +197,17 @@ impl Solution { * @return {number[]} */ var intersect = function (nums1, nums2) { - const counter = {}; - for (const num of nums1) { - counter[num] = (counter[num] || 0) + 1; + const cnt = {}; + for (const x of nums1) { + cnt[x] = (cnt[x] || 0) + 1; } - let res = []; - for (const num of nums2) { - if (counter[num] > 0) { - res.push(num); - counter[num] -= 1; + const ans = []; + for (const x of nums2) { + if (cnt[x]-- > 0) { + ans.push(x); } } - return res; + return ans; }; ``` @@ -216,30 +216,22 @@ var intersect = function (nums1, nums2) { ```cs public class Solution { public int[] Intersect(int[] nums1, int[] nums2) { - HashSet hs1 = new HashSet(nums1.Concat(nums2).ToArray()); - Dictionary dict = new Dictionary(); - List result = new List(); - - foreach (int x in hs1) { - dict[x] = 0; - } - + Dictionary cnt = new Dictionary(); foreach (int x in nums1) { - if (dict.ContainsKey(x)) { - dict[x] += 1; + if (cnt.ContainsKey(x)) { + cnt[x]++; } else { - dict[x] = 1; + cnt[x] = 1; } } - + List ans = new List(); foreach (int x in nums2) { - if (dict[x] > 0) { - result.Add(x); - dict[x] -=1; + if (cnt.ContainsKey(x) && cnt[x] > 0) { + ans.Add(x); + cnt[x]--; } } - - return result.ToArray(); + return ans.ToArray(); } } ``` @@ -254,17 +246,24 @@ class Solution { * @return Integer[] */ function intersect($nums1, $nums2) { - $rs = []; - for ($i = 0; $i < count($nums1); $i++) { - $hashtable[$nums1[$i]] += 1; + $cnt = []; + foreach ($nums1 as $x) { + if (isset($cnt[$x])) { + $cnt[$x]++; + } else { + $cnt[$x] = 1; + } } - for ($j = 0; $j < count($nums2); $j++) { - if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { - array_push($rs, $nums2[$j]); - $hashtable[$nums2[$j]] -= 1; + + $ans = []; + foreach ($nums2 as $x) { + if (isset($cnt[$x]) && $cnt[$x] > 0) { + $ans[] = $x; + $cnt[$x]--; } } - return $rs; + + return $ans; } } ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md index c09bd53cd6f6e..cbbb237e372a1 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md @@ -61,7 +61,13 @@ tags: -### Solution 1 +### Solution 1: Hash Table + +We can use a hash table $\text{cnt}$ to count the occurrences of each element in the array $\text{nums1}$. Then, we iterate through the array $\text{nums2}$. If an element $x$ is in $\text{cnt}$ and the occurrence of $x$ is greater than $0$, we add $x$ to the answer and then decrement the occurrence of $x$ by one. + +After the iteration is finished, we return the answer array. + +The time complexity is $O(m + n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the lengths of the arrays $\text{nums1}$ and $\text{nums2}$, respectively. @@ -70,13 +76,13 @@ tags: ```python class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: - counter = Counter(nums1) - res = [] - for num in nums2: - if counter[num] > 0: - res.append(num) - counter[num] -= 1 - return res + cnt = Counter(nums1) + ans = [] + for x in nums2: + if cnt[x]: + ans.append(x) + cnt[x] -= 1 + return ans ``` #### Java @@ -84,22 +90,17 @@ class Solution: ```java class Solution { public int[] intersect(int[] nums1, int[] nums2) { - Map counter = new HashMap<>(); - for (int num : nums1) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + int[] cnt = new int[1001]; + for (int x : nums1) { + ++cnt[x]; } - List t = new ArrayList<>(); - for (int num : nums2) { - if (counter.getOrDefault(num, 0) > 0) { - t.add(num); - counter.put(num, counter.get(num) - 1); + List ans = new ArrayList<>(); + for (int x : nums2) { + if (cnt[x]-- > 0) { + ans.add(x); } } - int[] res = new int[t.size()]; - for (int i = 0; i < res.length; ++i) { - res[i] = t.get(i); - } - return res; + return ans.stream().mapToInt(Integer::intValue).toArray(); } } ``` @@ -110,16 +111,17 @@ class Solution { class Solution { public: vector intersect(vector& nums1, vector& nums2) { - unordered_map counter; - for (int num : nums1) ++counter[num]; - vector res; - for (int num : nums2) { - if (counter[num] > 0) { - --counter[num]; - res.push_back(num); + unordered_map cnt; + for (int x : nums1) { + ++cnt[x]; + } + vector ans; + for (int x : nums2) { + if (cnt[x]-- > 0) { + ans.push_back(x); } } - return res; + return ans; } }; ``` @@ -127,19 +129,18 @@ public: #### Go ```go -func intersect(nums1 []int, nums2 []int) []int { - counter := make(map[int]int) - for _, num := range nums1 { - counter[num]++ +func intersect(nums1 []int, nums2 []int) (ans []int) { + cnt := map[int]int{} + for _, x := range nums1 { + cnt[x]++ } - var res []int - for _, num := range nums2 { - if counter[num] > 0 { - counter[num]-- - res = append(res, num) + for _, x := range nums2 { + if cnt[x] > 0 { + ans = append(ans, x) + cnt[x]-- } } - return res + return } ``` @@ -147,19 +148,17 @@ func intersect(nums1 []int, nums2 []int) []int { ```ts function intersect(nums1: number[], nums2: number[]): number[] { - const map = new Map(); - for (const num of nums1) { - map.set(num, (map.get(num) ?? 0) + 1); + const cnt: Record = {}; + for (const x of nums1) { + cnt[x] = (cnt[x] || 0) + 1; } - - const res = []; - for (const num of nums2) { - if (map.has(num) && map.get(num) !== 0) { - res.push(num); - map.set(num, map.get(num) - 1); + const ans: number[] = []; + for (const x of nums2) { + if (cnt[x]-- > 0) { + ans.push(x); } } - return res; + return ans; } ``` @@ -167,21 +166,23 @@ function intersect(nums1: number[], nums2: number[]): number[] { ```rust use std::collections::HashMap; + impl Solution { pub fn intersect(nums1: Vec, nums2: Vec) -> Vec { - let mut map = HashMap::new(); - for num in nums1.iter() { - *map.entry(num).or_insert(0) += 1; + let mut cnt = HashMap::new(); + for &x in &nums1 { + *cnt.entry(x).or_insert(0) += 1; } - - let mut res = vec![]; - for num in nums2.iter() { - if map.contains_key(num) && map.get(num).unwrap() != &0 { - map.insert(num, map.get(&num).unwrap() - 1); - res.push(*num); + let mut ans = Vec::new(); + for &x in &nums2 { + if let Some(count) = cnt.get_mut(&x) { + if *count > 0 { + ans.push(x); + *count -= 1; + } } } - res + ans } } ``` @@ -195,18 +196,17 @@ impl Solution { * @return {number[]} */ var intersect = function (nums1, nums2) { - const counter = {}; - for (const num of nums1) { - counter[num] = (counter[num] || 0) + 1; + const cnt = {}; + for (const x of nums1) { + cnt[x] = (cnt[x] || 0) + 1; } - let res = []; - for (const num of nums2) { - if (counter[num] > 0) { - res.push(num); - counter[num] -= 1; + const ans = []; + for (const x of nums2) { + if (cnt[x]-- > 0) { + ans.push(x); } } - return res; + return ans; }; ``` @@ -215,30 +215,22 @@ var intersect = function (nums1, nums2) { ```cs public class Solution { public int[] Intersect(int[] nums1, int[] nums2) { - HashSet hs1 = new HashSet(nums1.Concat(nums2).ToArray()); - Dictionary dict = new Dictionary(); - List result = new List(); - - foreach (int x in hs1) { - dict[x] = 0; - } - + Dictionary cnt = new Dictionary(); foreach (int x in nums1) { - if (dict.ContainsKey(x)) { - dict[x] += 1; + if (cnt.ContainsKey(x)) { + cnt[x]++; } else { - dict[x] = 1; + cnt[x] = 1; } } - + List ans = new List(); foreach (int x in nums2) { - if (dict[x] > 0) { - result.Add(x); - dict[x] -=1; + if (cnt.ContainsKey(x) && cnt[x] > 0) { + ans.Add(x); + cnt[x]--; } } - - return result.ToArray(); + return ans.ToArray(); } } ``` @@ -253,17 +245,24 @@ class Solution { * @return Integer[] */ function intersect($nums1, $nums2) { - $rs = []; - for ($i = 0; $i < count($nums1); $i++) { - $hashtable[$nums1[$i]] += 1; + $cnt = []; + foreach ($nums1 as $x) { + if (isset($cnt[$x])) { + $cnt[$x]++; + } else { + $cnt[$x] = 1; + } } - for ($j = 0; $j < count($nums2); $j++) { - if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { - array_push($rs, $nums2[$j]); - $hashtable[$nums2[$j]] -= 1; + + $ans = []; + foreach ($nums2 as $x) { + if (isset($cnt[$x]) && $cnt[$x] > 0) { + $ans[] = $x; + $cnt[$x]--; } } - return $rs; + + return $ans; } } ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp index d52d47c599fa9..98d931e9327bf 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp @@ -1,15 +1,16 @@ class Solution { public: vector intersect(vector& nums1, vector& nums2) { - unordered_map counter; - for (int num : nums1) ++counter[num]; - vector res; - for (int num : nums2) { - if (counter[num] > 0) { - --counter[num]; - res.push_back(num); + unordered_map cnt; + for (int x : nums1) { + ++cnt[x]; + } + vector ans; + for (int x : nums2) { + if (cnt[x]-- > 0) { + ans.push_back(x); } } - return res; + return ans; } }; \ No newline at end of file diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cs b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cs index 261a1a1603a7e..203a42cf15cbb 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cs +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cs @@ -1,28 +1,20 @@ public class Solution { public int[] Intersect(int[] nums1, int[] nums2) { - HashSet hs1 = new HashSet(nums1.Concat(nums2).ToArray()); - Dictionary dict = new Dictionary(); - List result = new List(); - - foreach (int x in hs1) { - dict[x] = 0; - } - + Dictionary cnt = new Dictionary(); foreach (int x in nums1) { - if (dict.ContainsKey(x)) { - dict[x] += 1; + if (cnt.ContainsKey(x)) { + cnt[x]++; } else { - dict[x] = 1; + cnt[x] = 1; } } - + List ans = new List(); foreach (int x in nums2) { - if (dict[x] > 0) { - result.Add(x); - dict[x] -=1; + if (cnt.ContainsKey(x) && cnt[x] > 0) { + ans.Add(x); + cnt[x]--; } } - - return result.ToArray(); + return ans.ToArray(); } -} +} \ No newline at end of file diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.go b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.go index 4f37f859860d0..afe6a4cdd3360 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.go +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.go @@ -1,14 +1,13 @@ -func intersect(nums1 []int, nums2 []int) []int { - counter := make(map[int]int) - for _, num := range nums1 { - counter[num]++ +func intersect(nums1 []int, nums2 []int) (ans []int) { + cnt := map[int]int{} + for _, x := range nums1 { + cnt[x]++ } - var res []int - for _, num := range nums2 { - if counter[num] > 0 { - counter[num]-- - res = append(res, num) + for _, x := range nums2 { + if cnt[x] > 0 { + ans = append(ans, x) + cnt[x]-- } } - return res + return } \ No newline at end of file diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.java b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.java index faf6aa9e18f38..084141c6b9348 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.java +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.java @@ -1,20 +1,15 @@ class Solution { public int[] intersect(int[] nums1, int[] nums2) { - Map counter = new HashMap<>(); - for (int num : nums1) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + int[] cnt = new int[1001]; + for (int x : nums1) { + ++cnt[x]; } - List t = new ArrayList<>(); - for (int num : nums2) { - if (counter.getOrDefault(num, 0) > 0) { - t.add(num); - counter.put(num, counter.get(num) - 1); + List ans = new ArrayList<>(); + for (int x : nums2) { + if (cnt[x]-- > 0) { + ans.add(x); } } - int[] res = new int[t.size()]; - for (int i = 0; i < res.length; ++i) { - res[i] = t.get(i); - } - return res; + return ans.stream().mapToInt(Integer::intValue).toArray(); } } \ No newline at end of file diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.js b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.js index 403625938f7c9..352a06dc517ca 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.js +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.js @@ -4,16 +4,15 @@ * @return {number[]} */ var intersect = function (nums1, nums2) { - const counter = {}; - for (const num of nums1) { - counter[num] = (counter[num] || 0) + 1; + const cnt = {}; + for (const x of nums1) { + cnt[x] = (cnt[x] || 0) + 1; } - let res = []; - for (const num of nums2) { - if (counter[num] > 0) { - res.push(num); - counter[num] -= 1; + const ans = []; + for (const x of nums2) { + if (cnt[x]-- > 0) { + ans.push(x); } } - return res; + return ans; }; diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php index 30f3bc9fdcb73..327961f5cc9f8 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php @@ -5,16 +5,23 @@ class Solution { * @return Integer[] */ function intersect($nums1, $nums2) { - $rs = []; - for ($i = 0; $i < count($nums1); $i++) { - $hashtable[$nums1[$i]] += 1; + $cnt = []; + foreach ($nums1 as $x) { + if (isset($cnt[$x])) { + $cnt[$x]++; + } else { + $cnt[$x] = 1; + } } - for ($j = 0; $j < count($nums2); $j++) { - if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { - array_push($rs, $nums2[$j]); - $hashtable[$nums2[$j]] -= 1; + + $ans = []; + foreach ($nums2 as $x) { + if (isset($cnt[$x]) && $cnt[$x] > 0) { + $ans[] = $x; + $cnt[$x]--; } } - return $rs; + + return $ans; } -} +} \ No newline at end of file diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py index d0f34bd9e2524..f7eb52a6e43bf 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py @@ -1,9 +1,9 @@ class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: - counter = Counter(nums1) - res = [] - for num in nums2: - if counter[num] > 0: - res.append(num) - counter[num] -= 1 - return res + cnt = Counter(nums1) + ans = [] + for x in nums2: + if cnt[x]: + ans.append(x) + cnt[x] -= 1 + return ans diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.rs b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.rs index c30786364d991..3c380019c261a 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.rs +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.rs @@ -1,18 +1,20 @@ use std::collections::HashMap; + impl Solution { pub fn intersect(nums1: Vec, nums2: Vec) -> Vec { - let mut map = HashMap::new(); - for num in nums1.iter() { - *map.entry(num).or_insert(0) += 1; + let mut cnt = HashMap::new(); + for &x in &nums1 { + *cnt.entry(x).or_insert(0) += 1; } - - let mut res = vec![]; - for num in nums2.iter() { - if map.contains_key(num) && map.get(num).unwrap() != &0 { - map.insert(num, map.get(&num).unwrap() - 1); - res.push(*num); + let mut ans = Vec::new(); + for &x in &nums2 { + if let Some(count) = cnt.get_mut(&x) { + if *count > 0 { + ans.push(x); + *count -= 1; + } } } - res + ans } } diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.ts b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.ts index f9480bd0ac923..a9091d893fce0 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.ts +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.ts @@ -1,15 +1,13 @@ function intersect(nums1: number[], nums2: number[]): number[] { - const map = new Map(); - for (const num of nums1) { - map.set(num, (map.get(num) ?? 0) + 1); + const cnt: Record = {}; + for (const x of nums1) { + cnt[x] = (cnt[x] || 0) + 1; } - - const res = []; - for (const num of nums2) { - if (map.has(num) && map.get(num) !== 0) { - res.push(num); - map.set(num, map.get(num) - 1); + const ans: number[] = []; + for (const x of nums2) { + if (cnt[x]-- > 0) { + ans.push(x); } } - return res; + return ans; }