题目来源于leetcode
余音绕梁,三日不绝
Problem: 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:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> result;
vector<int> tmp(k,0);
int i{0};
while(i >= 0 && tmp[0] <= n-k+1)
{
tmp[i]++;
if(tmp[i] > n)
{
i--;
}
else if(i == k-1)
{
result.push_back(tmp);
}
else
{
i++;
tmp[i] = tmp[i-1];
}
}
return result;
}
};