416. Partition Equal Subset Sum
这道题给了我们一个数组,问我们这个数组能不能分成两个非空子集合,使得两个子集合的元素之和相同
https://www.cnblogs.com/grandyang/p/5951422.html
class Solution { public: bool canPartition(vector<int>& nums) { int sum = 0; for(int i = 0;i < nums.size();i++) sum += nums[i]; if(sum%2) return false; int target = sum/2; vector<int> dp(target+1,false); dp[0] = true; for(int num : nums){ for(int i = target;i >= num;i--){ dp[i] = dp[i] || dp[i - num]; } } return dp[target]; } };