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], ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: void sub(vector<vector<int> >& ret,int n,int k){ if(n<k)return; if(k==0)return; vector<vector<int> >copy=ret; for(int i=0;i<ret.size();i++){ ret[i].push_back(n); } sub(ret,n-1,k-1); sub(copy,n-1,k); for(int i=0;i<copy.size();i++){ ret.push_back(copy[i]); } } vector<vector<int> > combine(int n, int k) { // Note: The Solution object is instantiated only once and is reused by each test case. vector<vector<int> >ret; ret.push_back(vector<int>()); sub(ret,n,k); vector<vector<int> >ret2; for(int i=0;i<ret.size();i++){ if(ret[i].size()==k){ for(int j=0;j<k/2;j++){ int temp; temp=ret[i][j]; ret[i][j]=ret[i][k-j-1]; ret[i][k-j-1]=temp; } ret2.push_back(ret[i]); } } return ret2; } };