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], ]
1 class Solution { 2 public: 3 vector<vector<int>> combine(int n, int k) { 4 vector<vector<int>> ans; 5 vector<int> temp; 6 if(k<=n&&k>0) 7 { 8 dep(n,1,k,temp,ans); 9 } 10 return ans; 11 } 12 void dep(int n,int p,int k,vector<int> &temp,vector<vector<int>> &ans) 13 { 14 if(k==0) 15 { 16 ans.push_back(temp); 17 return; 18 } 19 if(p<=n) 20 { 21 temp.push_back(p); 22 dep(n,p+1,k-1,temp,ans); 23 temp.erase(temp.end()-1); 24 dep(n,p+1,k,temp,ans); 25 } 26 27 } 28 };