-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |