Skip to content

Commit 6db7f6d

Browse files
authored
Create Partition Equal Subset Sum.java
1 parent 0576caf commit 6db7f6d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean canPartition(int[] nums) {
3+
int sum = 0;
4+
for (int num : nums) {
5+
sum += num;
6+
}
7+
if (sum % 2 == 1) {
8+
return false;
9+
}
10+
sum /= 2;
11+
int n = nums.length;
12+
boolean[] dp = new boolean[sum + 1];
13+
dp[0] = true;
14+
for (int num : nums) {
15+
for (int i = sum; i > 0; i--) {
16+
if (i >= num) {
17+
dp[i] = dp[i] || dp[i - num];
18+
}
19+
}
20+
}
21+
return dp[sum];
22+
}
23+
}

0 commit comments

Comments
 (0)