77 Combinations 组合
Description:
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
题目描述:
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例 :
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
思路:
回溯法
时间复杂度O(kC(n, k)), 空间复杂度O(k)
代码:
C++:
class Solution
{
public:
vector<vector<int>> combine(int n, int k)
{
vector<vector<int>> result;
vector<int> temp;
backtrack(result, temp, 1, ++n, k);
return result;
}
private:
void backtrack(vector<vector<int>> &result, vector<int> &temp, const int start, const int n, const int k)
{
if (k == 0)
{
result.push_back(temp);
return;
}
for (int i = start; i < n; i++)
{
temp.push_back(i);
backtrack(result, temp, i + 1, n, k - 1);
temp.pop_back();
}
}
};
Java:
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
backtrack(result, temp, 1, ++n, k);
return result;
}
private void backtrack(List<List<Integer>> result, List<Integer> temp, int start, int n, int k) {
if (k == 0) {
result.add(new ArrayList<Integer>(temp));
return;
}
for (int i = start; i < n; i++) {
temp.add(i);
backtrack(result, temp, i + 1, n, k - 1);
temp.remove(temp.size() - 1);
}
}
}
Python:
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
return list(itertools.combinations(range(1, n + 1), k))