416 Partition Equal Subset Sum

Posted lina2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了416 Partition Equal Subset Sum相关的知识,希望对你有一定的参考价值。

详见:https://leetcode.com/problems/partition-equal-subset-sum/description/

C++:

class Solution {
public:
    bool canPartition(vector<int>& nums)
    {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if (sum % 2 == 1)
        {
            return false;
        }
        int target = sum / 2;
        vector<bool> dp(target + 1, false);
        dp[0] = true;
        for (int i = 0; i < nums.size(); ++i)
        {
            for (int j = target; j >= nums[i]; --j) 
            {
                dp[j] = dp[j] || dp[j - nums[i]];
            }
        }
        return dp.back();
    }
};

 参考:http://www.cnblogs.com/grandyang/p/5951422.html

以上是关于416 Partition Equal Subset Sum的主要内容,如果未能解决你的问题,请参考以下文章

LN : leetcode 416 Partition Equal Subset Sum

416. Partition Equal Subset Sum

416. Partition Equal Subset Sum

Leetcode 416. Partition Equal Subset Sum

416. Partition Equal Subset Sum

算法: 等分子集总和416. Partition Equal Subset Sum