Combinations
来源:互联网 发布:淘宝联名信用卡怎么用 编辑:程序博客网 时间:2024/06/07 00:24
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的思路做,定义好start,end,depth以及max depth。
class Solution {public: vector<vector<int> > combine(int n, int k) { vector< vector<int> > result; if (n < k) { return result; } vector<int> v; combineUtil(result, v, 1, n, 0, k); return result; } void combineUtil(vector< vector<int> > &result, vector<int> &v, int start, int end, int depth, int maxdep) { if (depth == maxdep) { result.push_back(v); return; } for (int i = start; i <=end ; i++) { v.push_back(i); combineUtil(result, v, i+1, end, depth+1, maxdep); v.pop_back(); } } };
0 0
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- combinations
- Combinations
- Combinations
- Combinations
- Combinations
- poj 1953 Team Rankings
- 软件开发流程(转载)
- Leetcode: Merge Sorted Array
- 工作日报http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html
- 代码面试题:Find the first element in a given string A that also appears in another given string B
- Combinations
- osg入门系列7- 一个小房子
- SonarQube4.4+Jenkins进行代码检查实例之三-单元测试分析
- POJ3518_Prime Gap【素数】【水题】
- 服务端JS研究
- Node.js Async Tutorial
- POJ1595_Prime Cuts【素数】【水题】
- Leetcode: String to Integer
- Nodejs异步流程控制Async