返回介绍

Combination Sum

发布于 2025-02-22 13:01:31 字数 2648 浏览 0 评论 0 收藏 0

Source

Given a set of candidate numbers (C) and a target number (T),
find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.

For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

Have you met this question in a real interview? Yes
Example
given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

Note
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order.
(ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.

题解

Permutations 十分类似,区别在于剪枝函数不同。这里允许一个元素被多次使用,故递归时传入的索引值不自增,而是由 for 循环改变。

Java

public class Solution {
  /**
   * @param candidates: A list of integers
   * @param target:An integer
   * @return: A list of lists of integers
   */
  public List<List<Integer>> combinationSum(int[] candidates, int target) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    List<Integer> list = new ArrayList<Integer>();
    if (candidates == null) return result;

    Arrays.sort(candidates);
    helper(candidates, 0, target, list, result);

    return result;
  }

  private void helper(int[] candidates, int pos, int gap,
            List<Integer> list, List<List<Integer>> result) {

    if (gap == 0) {
      // add new object for result
      result.add(new ArrayList<Integer>(list));
      return;
    }

    for (int i = pos; i < candidates.length; i++) {
      // cut invalid candidate
      if (gap < candidates[i]) {
        return;
      }
      list.add(candidates[i]);
      helper(candidates, i, gap - candidates[i], list, result);
      list.remove(list.size() - 1);
    }
  }
}

源码分析

对数组首先进行排序是必须的,递归函数中本应该传入 target 作为入口参数,这里借用了 Soulmachine 的实现,使用 gap 更容易理解。注意在将临时 list 添加至 result 中时需要 new 一个新的对象。

复杂度分析

按状态数进行分析,时间复杂度 O(n!)O(n!)O(n!), 使用了 list 保存中间结果,空间复杂度 O(n)O(n)O(n).

Reference

  • Soulmachine 的 leetcode 题解

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文