Untitled
unknown
plain_text
a year ago
958 B
2
Indexable
Never
class Solution { public: vector<vector<int>> combine(int n, int k) { int i=0; vector<int>p(k,0); vector<vector<int>> res; while(i>=0){ p[i]++; if(p[i]>n) --i; else if(i==k-1) res.push_back(p); else{ i++; p[i]=p[i-1]; } } return res; } }; /* class Solution { public: void solve(int k,int start,vector<int>&comb,vector<vector<int>>&res,int n){ if(comb.size()==k) { res.push_back(comb); return; } for(int i=start;i<=n;i++){ comb.push_back(i); solve(k,i+1,comb,res,n); comb.pop_back(); } } vector<vector<int>> combine(int n, int k) { vector<vector<int>> res; vector<int> comb; solve(k,1,comb,res,n); return res; } }; */