LeetCode - Medium - 39. Combination Sum

╰+攻爆jí腚メ 2023-01-05 01:29 314阅读 0赞

Topic

  • Array
  • Backtracking

Description

https://leetcode.com/problems/combination-sum/

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

  1. Input: candidates = [2,3,6,7], target = 7
  2. Output: [[2,2,3],[7]]
  3. Explanation:
  4. 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
  5. 7 is a candidate, and 7 = 7.
  6. These are the only two combinations.

Example 2:

  1. Input: candidates = [2,3,5], target = 8
  2. Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3:

  1. Input: candidates = [2], target = 1
  2. Output: []

Example 4:

  1. Input: candidates = [1], target = 1
  2. Output: [[1]]

Example 5:

  1. Input: candidates = [1], target = 2
  2. Output: [[1,1]]

Constraints:

  • 1 <= candidates.length <= 30
  • 1 <= candidates[i] <= 200
  • All elements of candidates are distinct.
  • 1 <= target <= 500

Analysis

在这里插入图片描述
在这里插入图片描述

回溯算法:求组合总和(二)

Submission

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. public class CombinationSum {
  5. public List<List<Integer>> combinationSum1(int[] candidates, int target) {
  6. List<List<Integer>> result = new ArrayList<>();
  7. List<Integer> path = new ArrayList<>();
  8. backtracking1(path, candidates, target, 0, 0, result);
  9. return result;
  10. }
  11. private void backtracking1(List<Integer> path, int[] candidates, //
  12. int target, int sum, int startIndex, List<List<Integer>> result) {
  13. if (sum > target) {
  14. return;
  15. }
  16. if (sum == target) {
  17. result.add(new ArrayList<>(path));
  18. return;
  19. }
  20. for (int i = startIndex; i < candidates.length; i++) {
  21. sum += candidates[i];
  22. path.add(candidates[i]);
  23. backtracking1(path, candidates, target, sum, i, result);
  24. sum -= candidates[i];
  25. path.remove(path.size() - 1);
  26. }
  27. }
  28. // 剪枝优化后
  29. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  30. List<List<Integer>> result = new ArrayList<>();
  31. List<Integer> path = new ArrayList<>();
  32. Arrays.sort(candidates);
  33. backtracking2(path, candidates, target, 0, 0, result);
  34. return result;
  35. }
  36. private void backtracking2(List<Integer> path, int[] candidates, //
  37. int target, int sum, int startIndex, List<List<Integer>> result) {
  38. if (sum == target) {
  39. result.add(new ArrayList<>(path));
  40. return;
  41. }
  42. for (int i = startIndex; i < candidates.length && sum + candidates[i] <= target; i++) {
  43. sum += candidates[i];
  44. path.add(candidates[i]);
  45. backtracking2(path, candidates, target, sum, i, result);
  46. sum -= candidates[i];
  47. path.remove(path.size() - 1);
  48. }
  49. }
  50. }

Test

  1. import static org.junit.Assert.*;
  2. import java.util.Arrays;
  3. import org.hamcrest.collection.IsEmptyCollection;
  4. import org.hamcrest.collection.IsIterableContainingInAnyOrder;
  5. import org.junit.Test;
  6. public class CombinationSumTest {
  7. @Test
  8. @SuppressWarnings("unchecked")
  9. public void test1() {
  10. CombinationSum obj = new CombinationSum();
  11. assertThat(obj.combinationSum1(new int[] { 2, 3, 6, 7 }, 7), //
  12. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(2, 2, 3), Arrays.asList(7)));
  13. assertThat(obj.combinationSum1(new int[] { 2, 3, 5 }, 8), //
  14. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(2, 2, 2, 2), //
  15. Arrays.asList(2, 3, 3), Arrays.asList(3, 5)));
  16. assertThat(obj.combinationSum1(new int[] { 2 }, 1), IsEmptyCollection.empty());
  17. assertThat(obj.combinationSum1(new int[] { 1 }, 1), //
  18. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(1)));
  19. assertThat(obj.combinationSum1(new int[] { 1 }, 2), //
  20. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(1, 1)));
  21. }
  22. @Test
  23. @SuppressWarnings("unchecked")
  24. public void test2() {
  25. CombinationSum obj = new CombinationSum();
  26. assertThat(obj.combinationSum2(new int[] { 2, 3, 6, 7 }, 7), //
  27. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(2, 2, 3), Arrays.asList(7)));
  28. assertThat(obj.combinationSum2(new int[] { 2, 3, 5 }, 8), //
  29. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(2, 2, 2, 2), //
  30. Arrays.asList(2, 3, 3), Arrays.asList(3, 5)));
  31. assertThat(obj.combinationSum2(new int[] { 2 }, 1), IsEmptyCollection.empty());
  32. assertThat(obj.combinationSum2(new int[] { 1 }, 1), //
  33. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(1)));
  34. assertThat(obj.combinationSum2(new int[] { 1 }, 2), //
  35. IsIterableContainingInAnyOrder.containsInAnyOrder(Arrays.asList(1, 1)));
  36. }
  37. }

发表评论

表情:
评论列表 (有 0 条评论,314人围观)

还没有评论,来说两句吧...

相关阅读