You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Given a non-empty array 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.
* Note:
* Each of the array element will not exceed 100.
* The array size will not exceed 200.
* Example 1:
* Input: [1, 5, 11, 5]
* Output: true
* Explanation: The array can be partitioned as [1, 5, 5] and [11].
* Example 2:
* Input: [1, 2, 3, 5]
* Output: false
* Explanation: The array cannot be partitioned into equal sum subsets.
*/
/**
*
* 01背包问题
* @param {number[]} nums
* @return {boolean}
*/
var canPartition = function (nums) {
var sum = nums.reduce((a, b) => a + b, 0);
if (sum % 2) return false;
sum = sum / 2;
var n = nums.length;
var dp = [];
while (dp.push(new Array(sum + 1).fill(0)) < n + 1) ;