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 0e50ec5da521a..ac6b6a07e751d 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 @@ -52,6 +52,8 @@ nums 可以划分成 (2, 2) ,(3, 3) 和 (2, 2) ,满足所有要求。 +首先统计数组里面每个数字出现的次数。因为题目要求的数对属于将两个相等的元素放在一起,所以换句话说就是看每个数字出现的次数是不是偶数次。 + ### **Python3** @@ -59,7 +61,10 @@ nums 可以划分成 (2, 2) ,(3, 3) 和 (2, 2) ,满足所有要求。 ```python - +class Solution: + def divideArray(self, nums: List[int]) -> bool: + cnt = Counter(nums) + return all(v % 2 == 0 for v in cnt.values()) ``` ### **Java** 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 b72cb5bb25485..93202eea81a5e 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 @@ -21,7 +21,7 @@
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.@@ -31,7 +31,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.@@ -46,12 +46,17 @@ There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy ## Solutions +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. + ### **Python3** ```python - +class Solution: + def divideArray(self, nums: List[int]) -> bool: + cnt = Counter(nums) + return all(v % 2 == 0 for v in cnt.values()) ``` ### **Java** diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.py b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.py new file mode 100644 index 0000000000000..f416904caa8be --- /dev/null +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def divideArray(self, nums: List[int]) -> bool: + cnt = Counter(nums) + return all(v % 2 == 0 for v in cnt.values())