77. 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], ]
class Solution { public: void dfs(int n,int cur,int k,int x,vector<int>&v,vector<vector<int>>&u){ if(cur==k){ u.push_back(v); return ; } for(int i=x+1;i<=n;i++){ v.push_back(i); dfs(n,cur+1,k,i,v,u); v.pop_back(); } } vector<vector<int>> combine(int n, int k) { vector<vector<int>> u; vector<int>v; dfs(n,0,k,0,v,u); return u; } };
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: