Description:
You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that:
nums1.length == nums2.length == nums.length / 2.
nums1 should contain distinct elements.
nums2 should also contain distinct elements.
Return true if it is possible to split the array, and false otherwise.
Example 1:
Input: nums = [1,1,2,2,3,4]
Output: true
Explanation: One of the possible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4].
Example 2:
Input: nums = [1,1,1,1]
Output: false
Explanation: The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false.
Thinking:
题目要求判断是否可以将数组分成两个部分,使得两个部分的元素都是不同的。
最开始考虑的是Counter()
记录每个数字是否满足某个条件,如果不满足就返回False
。后面发现不需要Counter
,普通的字典记录,中途遇到某个数字大于2就返回False即可。
Code:
1 | class Solution: |
1 | class Solution: |