其他分享
首页 > 其他分享> > LeetCode 416. 分割等和子集

LeetCode 416. 分割等和子集

作者:互联网

01背包

const int N = 20010;
class Solution {
public:
    int dp[N];
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (int i = 0; i < nums.size(); i ++) sum += nums[i];

        if (sum % 2 != 0) 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;
    }
};

标签:return,target,nums,int,sum,416,子集,LeetCode,dp
来源: https://www.cnblogs.com/hjy94wo/p/16688441.html