Skip to content

Commit ea2fd13

Browse files
committed
combination sums
1 parent 7a82b2d commit ea2fd13

5 files changed

Lines changed: 123 additions & 5 deletions

File tree

‎src/ds/Test.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ds;
2+
3+
/**
4+
* Created by sherxon on 5/25/17.
5+
*/
6+
public class Test {
7+
8+
public static void main(String[] args) {
9+
HashedArrayTree hash = new HashedArrayTree();
10+
for (int i = 0; i < 500; i++) {
11+
hash.add(i);
12+
//hash.printStats();
13+
System.out.println(hash.get(i));
14+
}
15+
}
16+
17+
}

‎src/problems/Medium.txt‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,4 +1408,19 @@ Note:
14081408
The length of both lists will be in the range of [1, 1000].
14091409
The length of strings in both lists will be in the range of [1, 30].
14101410
The index is starting from 0 to the list length minus 1.
1411-
No duplicates in both lists.
1411+
No duplicates in both lists.
1412+
1413+
132) Combinations
1414+
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
1415+
1416+
For example,
1417+
If n = 4 and k = 2, a solution is:
1418+
1419+
[
1420+
[2,4],
1421+
[3,4],
1422+
[2,3],
1423+
[1,2],
1424+
[1,3],
1425+
[1,4],
1426+
]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problems.medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by sherxon on 6/3/17.
8+
*/
9+
public class Combination {
10+
11+
private static void exec(List<List<Integer>> list, boolean[] b, int k, int i) {
12+
if (k < 0) {
13+
return;
14+
}
15+
if (k == 0) {
16+
List<Integer> l = new ArrayList<>();
17+
for (int j = 1; j < b.length; j++) {
18+
if (b[j]) {
19+
l.add(j);
20+
}
21+
}
22+
list.add(l);
23+
}
24+
for (int j = i; j < b.length; j++) {
25+
if (!b[j]) {
26+
b[j] = true;
27+
exec(list, b, k - 1, j);
28+
b[j] = false;
29+
}
30+
}
31+
32+
}
33+
34+
public List<List<Integer>> combine(int n, int k) {
35+
List<List<Integer>> list = new ArrayList<>();
36+
37+
if (k <= 0 || k > n) {
38+
return list;
39+
}
40+
boolean[] b = new boolean[n + 1];
41+
exec(list, b, k, 1);
42+
return list;
43+
}
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problems.medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by sherxon on 6/3/17.
8+
*/
9+
public class CombinationSum {
10+
11+
public static void main(String[] args) {
12+
System.out.println(combinationSum(new int[]{7, 6, 3, 2}, 7));
13+
}
14+
15+
static public List<List<Integer>> combinationSum(int[] candidates, int target) {
16+
List<List<Integer>> list = new ArrayList<>();
17+
if (target == 0 || candidates.length == 0) {
18+
return list;
19+
}
20+
List<Integer> l = new ArrayList<>();
21+
go(candidates, target, target, list, l, 0);
22+
return list;
23+
}
24+
25+
private static void go(int[] candidates, int x, int target, List<List<Integer>> list,
26+
List<Integer> l, int i) {
27+
if (x < 0) {
28+
return;
29+
}
30+
if (x == 0) {
31+
list.add(l);
32+
} else {
33+
for (int j = 0; j < candidates.length; j++) {
34+
int xx = x - candidates[j];
35+
if (xx >= 0) {
36+
l.add(candidates[j]);
37+
go(candidates, xx, target, list, l, i);
38+
}
39+
40+
}
41+
}
42+
43+
}
44+
45+
}

‎src/problems/medium/CombinationSum3.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
*/
99
public class CombinationSum3 {
1010

11-
public static void main(String[] args) {
12-
System.out.println(combinationSum3(3, 7));
13-
}
14-
1511
static public List<List<Integer>> combinationSum3(int k, int n) {
1612
List<List<Integer>> list = new ArrayList<>();
1713
if (k == 0 || n == 0) {

0 commit comments

Comments
 (0)