波波老师,这样写,错误在哪?leetcode上416题:
class Solution {
public:
bool canPartition(vector& nums) {
int size = nums.size();
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += nums[i];
}
if (sum % 2 != 0)
{
return false;
}
int target = sum/2;
vector<vector<bool>> dp(size,(vector<bool>(target + 1, false)));
if (nums[0] <= target)
{
dp[0][nums[0]] = true;
}
if (size = 1)
{
return false;
}
for (int i = 1; i < size; i++)
{
for (int j = 0; j < target + 1; j++ )
{
dp[i][j] = dp[i - 1][j];
if (nums[i] == j)
{
dp[i][j] = true;
continue;
}
if (nums[i] < j)
{
dp[i][j] = (dp[i - 1][j- nums[i]] || dp[i - 1][j]);
}
}
}
return dp[size - 1][target];
}
};