class Solution {
public boolean canPartition(int[] nums)
{
int n = nums.length;
int sum = 0;
for(int i=0; i<n; i++)
{
sum+=nums[i];
}
if(sum%2 !=0 ) return false;
sum = sum/2;
boolean[][] possible = new boolean[n+1][sum+1];
for(int i=0; i<=n; i++) possible[i][0] = true;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=sum; j++)
{
if(nums[i-1] > j)
{
possible[i][j] = possible[i-1][j];
}
else
{
possible[i][j] = possible[i-1][j-nums[i-1]] || possible[i-1][j];
}
}
}
return possible[n][sum];
}
};
沒有留言:
張貼留言