Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example, If n = 4 and k = 2, a solution is:
代码语言:javascript复制[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
用1-n的数字组成长度为k的所有排列。
DFS搜索,也可以用迭代的方法解决
代码语言:javascript复制class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> result;
vector<int> temp(k,0);
int now=0;
while(now>=0)
{
temp[now] ;
if(temp[now]>n)
now--;
else if(now==k-1)
result.push_back(temp);
else
{
temp[now 1]=temp[now];
now ;
}
}
return result;
}
};