Skip to content

Commit d421285

Browse files
feat: add python solution to lc problem: No.2206 (#769)
No.2206.Divide Array Into Equal Pairs
1 parent a40dec1 commit d421285

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ nums 可以划分成 (2, 2) ,(3, 3) 和 (2, 2) ,满足所有要求。
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
首先统计数组里面每个数字出现的次数。因为题目要求的数对属于将两个相等的元素放在一起,所以换句话说就是看每个数字出现的次数是不是偶数次。
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
class Solution:
65+
def divideArray(self, nums: List[int]) -> bool:
66+
cnt = Counter(nums)
67+
return all(v % 2 == 0 for v in cnt.values())
6368
```
6469

6570
### **Java**

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<pre>
2222
<strong>Input:</strong> nums = [3,2,3,2,2,2]
2323
<strong>Output:</strong> true
24-
<strong>Explanation:</strong>
24+
<strong>Explanation:</strong>
2525
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
2626
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
2727
</pre>
@@ -31,7 +31,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
3131
<pre>
3232
<strong>Input:</strong> nums = [1,2,3,4]
3333
<strong>Output:</strong> false
34-
<strong>Explanation:</strong>
34+
<strong>Explanation:</strong>
3535
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
3636
</pre>
3737

@@ -46,12 +46,17 @@ There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy
4646

4747
## Solutions
4848

49+
The first step is to count the number of times each number appears in the array. Since the question asks for pairs of numbers that are part of putting two equal elements together, in other words to see if each number occurs an even number of times.
50+
4951
<!-- tabs:start -->
5052

5153
### **Python3**
5254

5355
```python
54-
56+
class Solution:
57+
def divideArray(self, nums: List[int]) -> bool:
58+
cnt = Counter(nums)
59+
return all(v % 2 == 0 for v in cnt.values())
5560
```
5661

5762
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def divideArray(self, nums: List[int]) -> bool:
3+
cnt = Counter(nums)
4+
return all(v % 2 == 0 for v in cnt.values())

0 commit comments

Comments
 (0)