# Problem 77: Combinations

> <https://leetcode.com/problems/combinations/>

## 思路

* 经典模板
* 想好退出条件：当 path 当中元素的个数等于 k 的时候，就可以退出了。

```java
public class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<Integer> path = new ArrayList<Integer>();
        List<List<Integer>> rst = new ArrayList(path);
        if (n < k) {
            return rst;
        }

        helper(n, k, 1, path, rst);

        return rst;
    }

    private void helper(int n, int k, int pos, List<Integer> path, List<List<Integer>> rst) {
        if (path.size() == k) {
            rst.add(new ArrayList(path));
            return;
        }
        for (int i = pos; i <= n; i++) {
            path.add(i);
            helper(n, k, i + 1, path, rst);
            path.remove(path.size() - 1);
        }
    }
}
```

## 易错点

1. 循环开始从 `for (int i = pos; i <= n; i++)`，而不是从 1 开始
2. helper 第二次调用的时候，从 i + 1 开始找，以免遇到重复元素


---

# Agent Instructions: 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:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_1_combination_and_permutation/combination/problem_77_combinations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
