题目描述:
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], ]
代码:
void get_combination(int n,int k,int start,vector<vector<int> > &combination,vector<int> vec) { if(k == 0) { combination.push_back(vec); return; } else { for(int i = start;i <= n;i++) { vector<int> temp = vec; temp.push_back(i); get_combination(n,k-1,start+1,combination,temp); } } } vector<vector<int> > Solution::combine(int n, int k) { vector<vector<int> > combination; vector<int> vec; get_combination(n,k,1,combination,vec); return combination; }
原文地址:http://blog.csdn.net/yao_wust/article/details/42191511