MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / backtrack

Method backtrack

java/0039-combination-sum.java:10–28  ·  view source on GitHub ↗
(
        int[] candidates,
        int target,
        List<List<Integer>> ans,
        List<Integer> cur,
        int index
    )

Source from the content-addressed store, hash-verified

8 }
9
10 public void backtrack(
11 int[] candidates,
12 int target,
13 List<List<Integer>> ans,
14 List<Integer> cur,
15 int index
16 ) {
17 if (target == 0) {
18 ans.add(new ArrayList(cur));
19 } else if (target < 0 || index >= candidates.length) {
20 return;
21 } else {
22 cur.add(candidates[index]);
23 backtrack(candidates, target - candidates[index], ans, cur, index);
24
25 cur.remove(cur.get(cur.size() - 1));
26 backtrack(candidates, target, ans, cur, index + 1);
27 }
28 }
29}

Callers 1

combinationSumMethod · 0.95

Calls 4

addMethod · 0.45
removeMethod · 0.45
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected