File tree Expand file tree Collapse file tree 3 files changed +81
-3
lines changed
Algorithms/416. Partition Equal Subset Sum Expand file tree Collapse file tree 3 files changed +81
-3
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 379
379
| 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|
380
380
| 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|
381
381
| 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|
383
383
| 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|
384
384
| 399| [ Evaluate Division] ( https://leetcode.com/problems/evaluate-division/ ) | Medium| noNote| no| no| no| no| no|
385
385
| 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
395
| 410| [ Split Array Largest Sum] ( https://leetcode.com/problems/split-array-largest-sum/ ) | Hard| noNote| no| no| no| no| no|
396
396
| 411| [ Minimum Unique Word Abbreviation] ( https://leetcode.com/problems/minimum-unique-word-abbreviation/ ) | Hard| noNote| no| no| no| no| no|
397
397
| 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|
399
399
| 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|
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments