-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
39 lines (39 loc) · 960 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Solution {
int[] nums;
int[] sums;
int l;
public boolean canPartition(int[] nums) {
Arrays.sort(nums);
this.nums=nums;
this.l=nums.length;
this.sums=new int[l];
sums[0]=nums[0];
for (int i = 1; i < l; i++) {
sums[i]=sums[i-1]+nums[i];
}
if (sums[l-1]%2==1) {
return false;
}
return canHalf(l-1, sums[l-1]/2);
}
public boolean canHalf(int index,int test){
if (test==0) {
return true;
}
if (test<0) {
return false;
}
for (int i = index; i >=0; i--) {
if (sums[i]<test) {
return false;
}else if (sums[i]==test) {
return true;
}else{
if (canHalf(i-1, test-nums[i])) {
return true;
}
}
}
return false;
}
}