3046. 分割数组

解题思路


只需要使用哈希表记录每个数组元素的出现次数,如果某个元素的次数>2,那么就不符合题目要求

实现代码

class Solution {
    public boolean isPossibleToSplit(int[] nums) {
        if (nums.length == 2) return true;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
            if (map.get(nums[i]) > 2) return false;
        }
        return true;
    }
}

复杂度分析


  • 时间复杂度:O(n)

  • 空间复杂度:O(n)