Skip to content

Commit bd6b18f

Browse files
committed
20161014
1 parent ccc9a97 commit bd6b18f

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Solution {
2+
int[] nums;
3+
int[] sums;
4+
int l;
5+
public boolean canPartition(int[] nums) {
6+
Arrays.sort(nums);
7+
this.nums=nums;
8+
this.l=nums.length;
9+
this.sums=new int[l];
10+
sums[0]=nums[0];
11+
for (int i = 1; i < l; i++) {
12+
sums[i]=sums[i-1]+nums[i];
13+
}
14+
if (sums[l-1]%2==1) {
15+
return false;
16+
}
17+
return canHalf(l-1, sums[l-1]/2);
18+
}
19+
public boolean canHalf(int index,int test){
20+
if (test==0) {
21+
return true;
22+
}
23+
if (test<0) {
24+
return false;
25+
}
26+
for (int i = index; i >=0; i--) {
27+
if (sums[i]<test) {
28+
return false;
29+
}else if (sums[i]==test) {
30+
return true;
31+
}else{
32+
if (canHalf(i-1, test-nums[i])) {
33+
return true;
34+
}
35+
}
36+
}
37+
return false;
38+
}
39+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
|394|[Decode String](https://leetcode.com/problems/decode-string/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/394. Decode String/Solution.java)|no|no|no|no|
380380
|395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/395. Longest Substring with At Least K Repeating Characters/Solution.java)|no|no|no|no|
381381
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/396. Rotate Function/Solution.java)|no|no|no|no|
382-
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/397. Integer Replacement/Solution.java)|no|no|no|no|
382+
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/397. Integer Replacement/Solution.java)|no|no|no|no|
383383
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/398. Random Pick Index/Solution.java)|no|no|no|no|
384384
|399|[Evaluate Division](https://leetcode.com/problems/evaluate-division/)|Medium|noNote|no|no|no|no|no|
385385
|400|[Nth Digit](https://leetcode.com/problems/nth-digit/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/400. Nth Digit/Solution.java)|no|no|no|no|
@@ -395,6 +395,6 @@
395395
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)|Hard|noNote|no|no|no|no|no|
396396
|411|[Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/)|Hard|noNote|no|no|no|no|no|
397397
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/415. Add Strings/Solution.java)|no|no|no|no|
398-
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)|Medium|noNote|no|no|no|no|no|
398+
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/416. Partition Equal Subset Sum/Solution.java)|no|no|no|no|
399399
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/417. Pacific Atlantic Water Flow/Solution.java)|no|no|no|no|
400-
|418|[Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/)|Hard|noNote|no|no|no|no|no|
400+
|418|[Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/)|Medium|noNote|no|no|no|no|no|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Solution {
2+
int[] nums;
3+
int[] sums;
4+
int l;
5+
public boolean canPartition(int[] nums) {
6+
Arrays.sort(nums);
7+
this.nums=nums;
8+
this.l=nums.length;
9+
this.sums=new int[l];
10+
sums[0]=nums[0];
11+
for (int i = 1; i < l; i++) {
12+
sums[i]=sums[i-1]+nums[i];
13+
}
14+
if (sums[l-1]%2==1) {
15+
return false;
16+
}
17+
return canHalf(l-1, sums[l-1]/2);
18+
}
19+
public boolean canHalf(int index,int test){
20+
if (test==0) {
21+
return true;
22+
}
23+
if (test<0) {
24+
return false;
25+
}
26+
for (int i = index; i >=0; i--) {
27+
if (sums[i]<test) {
28+
return false;
29+
}else if (sums[i]==test) {
30+
return true;
31+
}else{
32+
if (canHalf(i-1, test-nums[i])) {
33+
return true;
34+
}
35+
}
36+
}
37+
return false;
38+
}
39+
}

0 commit comments

Comments
 (0)