时间限制:1秒
空间限制:32768K
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> > ans; vector<int> temps; combineUtil(ans, temps,n,1,k); return ans; } void combineUtil(vector<vector<int>>& ans,vector<int>& temps,int n,int left,int k){ if (k == 0) { ans.push_back(temps); return; } else { for (int i=left;i<=n;i++) { temps.push_back(i); combineUtil(ans,temps,n,i+1,k-1); temps.pop_back(); } } } };