LeetCode 416. 分割等和子集
链接
class Solution {
public:bool canPartition(vector<int>& nums) {vector<int> dp(10001, 0);int sum = accumulate(nums.begin(), nums.end(), 0);if(sum % 2 == 1) {return false;}int target = sum / 2;for(int i = 0; i < nums.size(); i++) {for(int j = target; j >= nums[i]; j--) {dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);}}if(dp[target] == target) {return true;}return false;}
};