[leetcode] Combinations
来源:互联网 发布:淘宝台式机哪家 编辑:程序博客网 时间:2024/05/01 20:59
CombinationsApr 18 '12
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 { vector<vector<int> > ret;public: vector<vector<int> > combine(int n, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function ret.clear(); if(n<1 || k<1 || k>n) return ret; vector<int> v; run(v, 1, n, k); return ret; } void run(vector<int> &v, int dep, int n, int k) { if(v.size()==k) { ret.push_back(v); return; } if(k-v.size()>n-dep+1) return; run(v, dep+1, n, k); v.push_back(dep); run(v, dep+1, n, k); v.pop_back();};
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- LeetCode-Combinations
- [leetcode] Combinations
- LeetCode - Combinations
- 【Leetcode】Combinations
- Leetcode: Combinations
- 【leetcode】Combinations
- 关联、组合、聚合、依赖关系比较
- 黑马程序员——JAVA学习日志4
- java中的Vector类
- 《openssl 编程》之 RSA
- 用字母画等边三角形
- [leetcode] Combinations
- qmake 之 CONFIG 与 QT 乱谈
- 通过后续遍历建立二元查找树
- 悟透delphi 第四章 接口
- Linux基础(二):计算机启动流程
- Discuz2.5后台菜单增加方法
- 求二个日期之间的天数
- 悟透delphi 第五章 包
- K-based Numbers