Problem Description

Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Examples

Example 1:

Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:

Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.

Constraints:

解析

給定一個正整數陣列 nums,

要求寫一個演算法判斷是否有辦法把 nums 分成兩個 陣列 set1, set2 使得 sum(set1) = sum(set2)

要分成兩個陣列使得兩個陣列合相等

因為所有元素都是 > 0

代表兩個陣列合 = sum(nums)/2

因為可以分成兩個陣列代表 sum(nums) 一定要是偶數

其實只要找到其中一個陣列其中和 = sum(nums)/2 則另一個一定也是相同