From 31fb984c77598db953a04166c2c64b05286df2d6 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 21 Jan 2025 19:49:16 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2206 No.2206.Divide Array Into Equal Pairs --- .../README.md | 72 +++++++++++++++--- .../README_EN.md | 76 ++++++++++++++++--- .../Solution.cpp | 14 ++-- .../Solution.go | 10 +-- .../Solution.js | 11 +++ .../Solution.rs | 11 +++ .../Solution.ts | 7 ++ 7 files changed, 171 insertions(+), 30 deletions(-) create mode 100644 solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js create mode 100644 solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.rs create mode 100644 solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md index 8f888f89578a8..69f019780fa1d 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md @@ -69,7 +69,13 @@ nums 可以划分成 (2, 2) ,(3, 3) 和 (2, 2) ,满足所有要求。 -### 方法一 +### 方法一:计数 + +根据题目描述,只要数组中每个元素出现的次数都是偶数次,就可以将数组划分成 $n$ 个数对。 + +因此,我们可以用一个哈希表或者数组 $\textit{cnt}$ 记录每个元素出现的次数,然后遍历 $\textit{cnt}$,如果有任何一个元素出现的次数是奇数次,就返回 $\textit{false}$,否则返回 $\textit{true}$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 @@ -107,11 +113,15 @@ class Solution { class Solution { public: bool divideArray(vector& nums) { - vector cnt(510); - for (int& v : nums) ++cnt[v]; - for (int& v : cnt) - if (v % 2) + int cnt[510]{}; + for (int x : nums) { + ++cnt[x]; + } + for (int i = 1; i <= 500; ++i) { + if (cnt[i] % 2) { return false; + } + } return true; } }; @@ -121,12 +131,12 @@ public: ```go func divideArray(nums []int) bool { - cnt := make([]int, 510) - for _, v := range nums { - cnt[v]++ + cnt := [510]int{} + for _, x := range nums { + cnt[x]++ } for _, v := range cnt { - if v%2 == 1 { + if v%2 != 0 { return false } } @@ -134,6 +144,50 @@ func divideArray(nums []int) bool { } ``` +#### TypeScript + +```ts +function divideArray(nums: number[]): boolean { + const cnt: Record = {}; + for (const x of nums) { + cnt[x] = (cnt[x] || 0) + 1; + } + return Object.values(cnt).every(x => x % 2 === 0); +} +``` + +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn divide_array(nums: Vec) -> bool { + let mut cnt = HashMap::new(); + for x in nums { + *cnt.entry(x).or_insert(0) += 1; + } + cnt.values().all(|&v| v % 2 == 0) + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {boolean} + */ +var divideArray = function (nums) { + const cnt = {}; + for (const x of nums) { + cnt[x] = (cnt[x] || 0) + 1; + } + return Object.values(cnt).every(x => x % 2 === 0); +}; +``` + diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md index 2049b988e5c04..35b9c46b2b4cd 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md @@ -38,7 +38,7 @@ tags:
 Input: nums = [3,2,3,2,2,2]
 Output: true
-Explanation: 
+Explanation:
 There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
 If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
 
@@ -48,7 +48,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
 Input: nums = [1,2,3,4]
 Output: false
-Explanation: 
+Explanation:
 There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
 
@@ -67,7 +67,13 @@ There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy -### Solution 1 +### Solution 1: Counting + +According to the problem description, as long as each element in the array appears an even number of times, the array can be divided into $n$ pairs. + +Therefore, we can use a hash table or an array $\textit{cnt}$ to record the number of occurrences of each element, then traverse $\textit{cnt}$. If any element appears an odd number of times, return $\textit{false}$; otherwise, return $\textit{true}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -105,11 +111,15 @@ class Solution { class Solution { public: bool divideArray(vector& nums) { - vector cnt(510); - for (int& v : nums) ++cnt[v]; - for (int& v : cnt) - if (v % 2) + int cnt[510]{}; + for (int x : nums) { + ++cnt[x]; + } + for (int i = 1; i <= 500; ++i) { + if (cnt[i] % 2) { return false; + } + } return true; } }; @@ -119,12 +129,12 @@ public: ```go func divideArray(nums []int) bool { - cnt := make([]int, 510) - for _, v := range nums { - cnt[v]++ + cnt := [510]int{} + for _, x := range nums { + cnt[x]++ } for _, v := range cnt { - if v%2 == 1 { + if v%2 != 0 { return false } } @@ -132,6 +142,50 @@ func divideArray(nums []int) bool { } ``` +#### TypeScript + +```ts +function divideArray(nums: number[]): boolean { + const cnt: Record = {}; + for (const x of nums) { + cnt[x] = (cnt[x] || 0) + 1; + } + return Object.values(cnt).every(x => x % 2 === 0); +} +``` + +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn divide_array(nums: Vec) -> bool { + let mut cnt = HashMap::new(); + for x in nums { + *cnt.entry(x).or_insert(0) += 1; + } + cnt.values().all(|&v| v % 2 == 0) + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {boolean} + */ +var divideArray = function (nums) { + const cnt = {}; + for (const x of nums) { + cnt[x] = (cnt[x] || 0) + 1; + } + return Object.values(cnt).every(x => x % 2 === 0); +}; +``` + diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.cpp b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.cpp index cfd673b74c6b5..2ef2247a9e4af 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.cpp +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.cpp @@ -1,11 +1,15 @@ class Solution { public: bool divideArray(vector& nums) { - vector cnt(510); - for (int& v : nums) ++cnt[v]; - for (int& v : cnt) - if (v % 2) + int cnt[510]{}; + for (int x : nums) { + ++cnt[x]; + } + for (int i = 1; i <= 500; ++i) { + if (cnt[i] % 2) { return false; + } + } return true; } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.go b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.go index dc2d5e79859dc..cba8acbc5f70e 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.go +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.go @@ -1,12 +1,12 @@ func divideArray(nums []int) bool { - cnt := make([]int, 510) - for _, v := range nums { - cnt[v]++ + cnt := [510]int{} + for _, x := range nums { + cnt[x]++ } for _, v := range cnt { - if v%2 == 1 { + if v%2 != 0 { return false } } return true -} \ No newline at end of file +} diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js new file mode 100644 index 0000000000000..6ec8af3eaa8a8 --- /dev/null +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var divideArray = function (nums) { + const cnt = {}; + for (const x of nums) { + cnt[x] = (cnt[x] || 0) + 1; + } + return Object.values(cnt).every(x => x % 2 === 0); +}; diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.rs b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.rs new file mode 100644 index 0000000000000..a654d060b45af --- /dev/null +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.rs @@ -0,0 +1,11 @@ +use std::collections::HashMap; + +impl Solution { + pub fn divide_array(nums: Vec) -> bool { + let mut cnt = HashMap::new(); + for x in nums { + *cnt.entry(x).or_insert(0) += 1; + } + cnt.values().all(|&v| v % 2 == 0) + } +} diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts new file mode 100644 index 0000000000000..a6604adfe2ae3 --- /dev/null +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts @@ -0,0 +1,7 @@ +function divideArray(nums: number[]): boolean { + const cnt: Record = {}; + for (const x of nums) { + cnt[x] = (cnt[x] || 0) + 1; + } + return Object.values(cnt).every(x => x % 2 === 0); +}