class Solution {
List<List<Integer>> result;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
result = new ArrayList<>(151);
dfs(candidates, target, new ArrayList<>(), 0);
return result;
}
private void dfs(int[] candidates, int target, List<Integer> path, int idx) {
if (target < 0) {
return;
} else if (target == 0) {
result.add(new ArrayList<>(path));
return;
}
if(idx >= candidates.length) {
return;
}
dfs(candidates, target, path, idx + 1);
int cnt = 1;
while (target >= candidates[idx]) {
for (int i = 0; i < cnt; i++) {
path.add(candidates[idx]);
}
target -= candidates[idx];
dfs(candidates, target, path, idx + 1);
for (int i = 0; i < cnt; i++) {
path.remove(path.size() - 1);
}
cnt++;
}
}
}
10-10
155