北川广海の梦

北川广海の梦

一道经典的回溯递归算法题

194
2020-03-27

LeetCode39号问题
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

所有数字(包括 target)都是正整数。
解集不能包含重复的组合。 
示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

思路:

  1. 对于目标数字,target,验证数组中的每一个元素与target的差值,是否在数组中。
  2. 如果差值存在,则说明产生差值的数即为目标集合。
  3. 如果差值不存在数组中,且差值大于0,则以差值作为新的target,并将当前元素压入栈中,继续验证。
  4. 直到target与元素差值为0,此时栈中所有元素与当前元素,即为目标集合。
  5. 如果target与元素差值小于0,则说明无法根据此元素找到目标集合,结束寻找。
  6. 剪枝,为避免重复,将数组排序之后,每次都从后往前找,并且只从当前元素开始找,所以需要记录当前元素位置。
 public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();

        Stack<Integer> stack = new Stack<>();
        Search(result, candidates, target, 0, stack);
        return result;
    }

    public static void Search(List<List<Integer>> result, int[] candidates, int target, int begin, Stack<Integer> stack) {
        for (int i = begin; i < candidates.length; i++)
            if (target - candidates[i] == 0) {
                stack.push(candidates[i]);
                result.add(new ArrayList<>(stack));
                stack.pop();
                return;
            } else if (target - candidates[i] < 0) {
                return;
            } else if (target - candidates[i] > 0) {
                stack.push(candidates[i]);
                Search(result, candidates, target - candidates[i], i, stack);
                stack.pop();
            }
    }

图示
剪枝