• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Leetcode 416 划分相等和子集dp


Recommended Posts

地址https://leetcode-cn.com/problems/partition-equal-subset-sum/

给你一个只包含正整数的非空数组nums。

请判断这个数组是否可以分成两个子集,使得两个子集的元素之和相等。

示例1:

输入:nums=[1,5,11,5]

输出:真

说明:数组可以分为[1,5,5]和[11]。

示例2:

输入:nums=[1,2,3,5]

输出:假

说明:一个数组不能分成两个元素和相等的子集。

提示:

1=nums.length=200

1=nums=100

解释

01背包问题

pm5quguj1m03424.png

类别解决方案{

公共:

bool canPartition(矢量数字){

int sum=0;

对于(auto e : nums){ sum=e;}

if(sum % 2==1){返回false}

sum/=2;

int DP[20010];memset(dp,0,sizeof DP);DP[0]=1;

for(int I=0;I nums . size();i ){

int val=nums

for(int j=sum;j=0;j - ){

if(j-val=0 DP[j-val]==1){ DP[j]=1;}

}

}

return DP[sum]==1;

}

};

我的视频问题解决空间

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now