> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/leetcode/backtracking/39-combination-sum.md).

# 39 Combination Sum

## 39. [Combination Sum](https://leetcode.com/problems/combination-sum/description/)

## 1. Question

Given a **set** of candidate numbers (**C**)**(without duplicates)**&#x61;nd 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.

**Note:**

* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.

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

```
[
  [7],
  [2, 2, 3]
]
```

## 2. Implementation

**(1) Backtracking**

```java
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> combinations = new ArrayList<>();
        getCombinations(candidates, 0, 0, target, combinations, res);
        return res;
    }

    public void getCombinations(int[] candidates, int index, int sum, int target, List<Integer> combinations, List<List<Integer>> res) {
        if (sum > target) {
            return;
        }

        if (sum == target) {
            res.add(new ArrayList<>(combinations));
            return;
        }

        for (int i = index; i < candidates.length; i++) {
            combinations.add(candidates[i]);
            getCombinations(candidates, i, sum + candidates[i], target, combinations, res);
            combinations.remove(combinations.size() - 1);
        }
    }
}
```

## 3. Time & Space Complexity

**Backtracking: 时间复杂度O(2^n), 空间复杂度O(2^n)**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/backtracking/39-combination-sum.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
