Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 80367ae

Browse files
authored
feat: add solutions to lc problem: No.2206 (doocs#3979)
No.2206.Divide Array Into Equal Pairs
1 parent 82e2e3e commit 80367ae

File tree

7 files changed

+171
-30
lines changed

7 files changed

+171
-30
lines changed

solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ nums 可以划分成 (2, 2) ,(3, 3) 和 (2, 2) ,满足所有要求。
6969

7070
<!-- solution:start -->
7171

72-
### 方法一
72+
### 方法一:计数
73+
74+
根据题目描述,只要数组中每个元素出现的次数都是偶数次,就可以将数组划分成 $n$ 个数对。
75+
76+
因此,我们可以用一个哈希表或者数组 $\textit{cnt}$ 记录每个元素出现的次数,然后遍历 $\textit{cnt}$,如果有任何一个元素出现的次数是奇数次,就返回 $\textit{false}$,否则返回 $\textit{true}$。
77+
78+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
7379

7480
<!-- tabs:start -->
7581

@@ -107,11 +113,15 @@ class Solution {
107113
class Solution {
108114
public:
109115
bool divideArray(vector<int>& nums) {
110-
vector<int> cnt(510);
111-
for (int& v : nums) ++cnt[v];
112-
for (int& v : cnt)
113-
if (v % 2)
116+
int cnt[510]{};
117+
for (int x : nums) {
118+
++cnt[x];
119+
}
120+
for (int i = 1; i <= 500; ++i) {
121+
if (cnt[i] % 2) {
114122
return false;
123+
}
124+
}
115125
return true;
116126
}
117127
};
@@ -121,19 +131,63 @@ public:
121131
122132
```go
123133
func divideArray(nums []int) bool {
124-
cnt := make([]int, 510)
125-
for _, v := range nums {
126-
cnt[v]++
134+
cnt := [510]int{}
135+
for _, x := range nums {
136+
cnt[x]++
127137
}
128138
for _, v := range cnt {
129-
if v%2 == 1 {
139+
if v%2 != 0 {
130140
return false
131141
}
132142
}
133143
return true
134144
}
135145
```
136146

147+
#### TypeScript
148+
149+
```ts
150+
function divideArray(nums: number[]): boolean {
151+
const cnt: Record<number, number> = {};
152+
for (const x of nums) {
153+
cnt[x] = (cnt[x] || 0) + 1;
154+
}
155+
return Object.values(cnt).every(x => x % 2 === 0);
156+
}
157+
```
158+
159+
#### Rust
160+
161+
```rust
162+
use std::collections::HashMap;
163+
164+
impl Solution {
165+
pub fn divide_array(nums: Vec<i32>) -> bool {
166+
let mut cnt = HashMap::new();
167+
for x in nums {
168+
*cnt.entry(x).or_insert(0) += 1;
169+
}
170+
cnt.values().all(|&v| v % 2 == 0)
171+
}
172+
}
173+
```
174+
175+
#### JavaScript
176+
177+
```js
178+
/**
179+
* @param {number[]} nums
180+
* @return {boolean}
181+
*/
182+
var divideArray = function (nums) {
183+
const cnt = {};
184+
for (const x of nums) {
185+
cnt[x] = (cnt[x] || 0) + 1;
186+
}
187+
return Object.values(cnt).every(x => x % 2 === 0);
188+
};
189+
```
190+
137191
<!-- tabs:end -->
138192

139193
<!-- solution:end -->

solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tags:
3838
<pre>
3939
<strong>Input:</strong> nums = [3,2,3,2,2,2]
4040
<strong>Output:</strong> true
41-
<strong>Explanation:</strong>
41+
<strong>Explanation:</strong>
4242
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
4343
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
4444
</pre>
@@ -48,7 +48,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
4848
<pre>
4949
<strong>Input:</strong> nums = [1,2,3,4]
5050
<strong>Output:</strong> false
51-
<strong>Explanation:</strong>
51+
<strong>Explanation:</strong>
5252
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
5353
</pre>
5454

@@ -67,7 +67,13 @@ There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Counting
71+
72+
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.
73+
74+
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}$.
75+
76+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
7177

7278
<!-- tabs:start -->
7379

@@ -105,11 +111,15 @@ class Solution {
105111
class Solution {
106112
public:
107113
bool divideArray(vector<int>& nums) {
108-
vector<int> cnt(510);
109-
for (int& v : nums) ++cnt[v];
110-
for (int& v : cnt)
111-
if (v % 2)
114+
int cnt[510]{};
115+
for (int x : nums) {
116+
++cnt[x];
117+
}
118+
for (int i = 1; i <= 500; ++i) {
119+
if (cnt[i] % 2) {
112120
return false;
121+
}
122+
}
113123
return true;
114124
}
115125
};
@@ -119,19 +129,63 @@ public:
119129
120130
```go
121131
func divideArray(nums []int) bool {
122-
cnt := make([]int, 510)
123-
for _, v := range nums {
124-
cnt[v]++
132+
cnt := [510]int{}
133+
for _, x := range nums {
134+
cnt[x]++
125135
}
126136
for _, v := range cnt {
127-
if v%2 == 1 {
137+
if v%2 != 0 {
128138
return false
129139
}
130140
}
131141
return true
132142
}
133143
```
134144

145+
#### TypeScript
146+
147+
```ts
148+
function divideArray(nums: number[]): boolean {
149+
const cnt: Record<number, number> = {};
150+
for (const x of nums) {
151+
cnt[x] = (cnt[x] || 0) + 1;
152+
}
153+
return Object.values(cnt).every(x => x % 2 === 0);
154+
}
155+
```
156+
157+
#### Rust
158+
159+
```rust
160+
use std::collections::HashMap;
161+
162+
impl Solution {
163+
pub fn divide_array(nums: Vec<i32>) -> bool {
164+
let mut cnt = HashMap::new();
165+
for x in nums {
166+
*cnt.entry(x).or_insert(0) += 1;
167+
}
168+
cnt.values().all(|&v| v % 2 == 0)
169+
}
170+
}
171+
```
172+
173+
#### JavaScript
174+
175+
```js
176+
/**
177+
* @param {number[]} nums
178+
* @return {boolean}
179+
*/
180+
var divideArray = function (nums) {
181+
const cnt = {};
182+
for (const x of nums) {
183+
cnt[x] = (cnt[x] || 0) + 1;
184+
}
185+
return Object.values(cnt).every(x => x % 2 === 0);
186+
};
187+
```
188+
135189
<!-- tabs:end -->
136190

137191
<!-- solution:end -->
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
class Solution {
22
public:
33
bool divideArray(vector<int>& nums) {
4-
vector<int> cnt(510);
5-
for (int& v : nums) ++cnt[v];
6-
for (int& v : cnt)
7-
if (v % 2)
4+
int cnt[510]{};
5+
for (int x : nums) {
6+
++cnt[x];
7+
}
8+
for (int i = 1; i <= 500; ++i) {
9+
if (cnt[i] % 2) {
810
return false;
11+
}
12+
}
913
return true;
1014
}
11-
};
15+
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
func divideArray(nums []int) bool {
2-
cnt := make([]int, 510)
3-
for _, v := range nums {
4-
cnt[v]++
2+
cnt := [510]int{}
3+
for _, x := range nums {
4+
cnt[x]++
55
}
66
for _, v := range cnt {
7-
if v%2 == 1 {
7+
if v%2 != 0 {
88
return false
99
}
1010
}
1111
return true
12-
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var divideArray = function (nums) {
6+
const cnt = {};
7+
for (const x of nums) {
8+
cnt[x] = (cnt[x] || 0) + 1;
9+
}
10+
return Object.values(cnt).every(x => x % 2 === 0);
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn divide_array(nums: Vec<i32>) -> bool {
5+
let mut cnt = HashMap::new();
6+
for x in nums {
7+
*cnt.entry(x).or_insert(0) += 1;
8+
}
9+
cnt.values().all(|&v| v % 2 == 0)
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function divideArray(nums: number[]): boolean {
2+
const cnt: Record<number, number> = {};
3+
for (const x of nums) {
4+
cnt[x] = (cnt[x] || 0) + 1;
5+
}
6+
return Object.values(cnt).every(x => x % 2 === 0);
7+
}

0 commit comments

Comments
 (0)