-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum.java
More file actions
45 lines (39 loc) · 1.4 KB
/
combinationSum.java
File metadata and controls
45 lines (39 loc) · 1.4 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
import java.util.ArrayList;
import java.util.List;
/**
* Author : WindAsMe
* File : combinationSum.java
* Time : Create on 18-5-29
* Location : ../Home/JavaForLeeCode2/combinationSum.java
* Function : LeeCode No.39
*/
public class combinationSum {
private static List<List<Integer>> combinationSumResult(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
if (candidates.length == 0){
return result;
} else {
List<Integer> temp = new ArrayList<>();
int tempSum = 0;
addList(result, temp, tempSum, candidates, target);
System.out.println(result.size());
}
return null;
}
private static void addList(List<List<Integer>> result, List<Integer> temp, int tempSum, int[] candidates, int target){
for (int candidate : candidates) {
if (tempSum + candidate == target) {
temp.add(candidate);
result.add(temp);
temp.remove(temp.size() - 1);
} else if (tempSum + candidate < target) {
temp.add(candidate);
addList(result, temp, tempSum + candidate, candidates, target);
}
}
}
public static void main(String[] args){
int[] nums = {1, 2, 3, 4, 5};
List<List<Integer>> list = combinationSumResult(nums, 4);
}
}