-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
CombinationSum3.java
46 lines (39 loc) · 946 Bytes
/
CombinationSum3.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
40
41
42
43
44
45
46
package problems.medium;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sherxon on 5/28/17.
*/
public class CombinationSum3 {
static public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> list = new ArrayList<>();
if (k == 0 || n == 0) {
return list;
}
boolean[] a = new boolean[10];
go(k, n, list, a, 1);
return list;
}
static void go(int k, int n, List<List<Integer>> list, boolean[] a, int j) {
if (n < 0 || k < 0) {
return;
}
if (n == 0 && k == 0) {
List<Integer> l = new ArrayList<>();
for (int i = 0; i < a.length; i++) {
if (a[i]) {
l.add(i);
}
}
list.add(l);
} else {
for (int i = j; i < a.length; i++) {
if (!a[i] && n - i >= 0 && k > 0) {
a[i] = true;
go(k - 1, n - i, list, a, i + 1);
a[i] = false;
}
}
}
}
}