-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSumIII_216.java
More file actions
54 lines (49 loc) · 1.79 KB
/
Copy pathCombinationSumIII_216.java
File metadata and controls
54 lines (49 loc) · 1.79 KB
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
47
48
49
50
51
52
53
54
package com.leetcode.array;
import java.util.ArrayList;
import java.util.List;
/**
* Created by charles on 12/17/16.
* Find all possible combinations of k numbers that add up to a number n,
* given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers
*/
public class CombinationSumIII_216 {
/**
* Thought:
* use BackTracking (recursion) to find result
*/
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
combine(res, new ArrayList<Integer>(), k, 1, n);
return res;
}
private void combine(List<List<Integer>> res,
List<Integer> list,
int k,
int start,
int remainder) {
if (list.size() == k && remainder == 0) {
// must to wrap last result from recursion into new list after creating new instance
// otherwise only add empty list
res.add(new ArrayList<Integer>(list));
return;
}
// traversing all possibility from start to 9
for (int i = start; i <= 9; i++) {
list.add(i);
// recursively to try this new added number
combine(res, list, k, i+1, remainder - i);
// after recursion, must remove last added from temp list
list.remove(list.size() - 1);
}
}
public static void main(String[] args) {
CombinationSumIII_216 c = new CombinationSumIII_216();
print(c.combinationSum3(4, 10));
}
private static void print(List<List<Integer>> res) {
for (List<Integer> l : res) {
l.stream().forEach(t -> System.out.print(t + ","));
System.out.println("");
}
}
}