-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathmintheon.java
38 lines (31 loc) · 919 Bytes
/
mintheon.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
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
class Solution {
/**
시간복잡도: O(2^n)
공간복잡도: O(n)
*/
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<>();
Deque<Integer> nums = new ArrayDeque<>();
backtracking(candidates, answer, nums, target, 0, 0);
return answer;
}
protected void backtracking(int[] candidates, List<List<Integer>> answer, Deque<Integer> nums, int target, int start, int total) {
if(total > target) {
return;
}
if (total == target) {
answer.add(new ArrayList<>(nums));
return;
}
for(int i = start; i < candidates.length; i++) {
int num = candidates[i];
nums.push(num);
backtracking(candidates, answer, nums, target, i, total + num);
nums.pop();
}
}
}