Skip to content

Commit

Permalink
Add BackTrackingTest
Browse files Browse the repository at this point in the history
  • Loading branch information
bky373 committed Jun 25, 2024
1 parent 59c3133 commit cf9124f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/test/java/BackTrackingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class BackTrackingTest {

/*
* https://leetcode.com/problems/combination-sum/
*/
@Test
void combinationSum() {
int[] candidates = {2, 3, 6, 7};
int target = 7;

List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> combination = new LinkedList<>();
backtrack(candidates, target, combination, 0, result);
System.out.println("result = " + result);
}

void backtrack(int[] candidates, int remain, LinkedList<Integer> combination, int start, List<List<Integer>> result) {
if (remain == 0) {
result.add(new ArrayList<>(combination));
} else if (remain < 0) {
return;
}

for (int i = start; i < candidates.length; i++) {
combination.add(candidates[i]);
backtrack(candidates, remain - candidates[i], combination, i, result);
combination.removeLast();
}
}
}

0 comments on commit cf9124f

Please sign in to comment.