Leetcode:Combinations 组合
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 _combination(int n,int k,int start,vector<int> temp,vector<vector<int> >& result){ if(k==0){ vector<int>::iterator it = temp.begin(); for(;it!=temp.end();it++) cout<<*it<<" "; cout<<endl; result.push_back(temp); } for(int i=start;i<=n;++i){ temp.push_back(i); _combination(n,k-1,i+1,temp,result); temp.pop_back(); } } vector<vector<int> > combination(int n,int k){ vector<int> temp; vector<vector<int> > result; _combination(n,k,1,temp,result);
return result; }