标签:
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], ]题意:求组合。
思路:dfs搜索的类型,类似我们自己计算组合的情况,为了不重复我们必须不断的向前走
class Solution { public: vector<vector<int> > ans; vector<int> tmp; void dfs(int cur, int dep, int n, int s) { if (cur == dep) { ans.push_back(tmp); return; } for (int i = s; i <= n; i++) { tmp[cur] = i; dfs(cur+1, dep, n, i+1); } } vector<vector<int> > combine(int n, int k) { tmp.resize(k); ans.clear(); dfs(0, k, n, 1); return ans; } };
标签:
原文地址:http://blog.csdn.net/u011345136/article/details/44922319