LeetCode题解:Combinations
来源:互联网 发布:艾默生网络能源 编辑:程序博客网 时间:2024/05/16 12:36
Combinations
Given two integers n and k, return all possible combinations ofk 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: int max; int unused; vector<vector<int>> ret; vector<int> used; void try_combine(int i) { if (unused == 0) { ret.push_back(used); return; } if (i > max) return; try_combine(i + 1); used.push_back(i); --unused; try_combine(i + 1); used.pop_back(); ++unused; } vector<vector<int> > combine(int n, int k) { ret.clear(); used.clear(); max = n; unused = k; try_combine(1); return ret; }};
- LeetCode题解:Combinations
- [题解][LeetCode][Combinations]
- LeetCode 题解(165): Combinations
- LeetCode题解——Combinations
- LeetCode 题解(253) : Factor Combinations
- LeetCode题解:Letter Combinations of a Phone Number
- leetcode 题解 || Letter Combinations of a Phone Number 问题
- LeetCode 题解(77): Letter Combinations of a Phone Number
- LeetCode题解——Letter Combinations of a Phone Number
- LeetCode题解:Letter Combinations of a Phone Number
- 【leetcode题解】【M】【67】Letter Combinations of a Phone Number
- LEETCODE 17 Letter Combinations of a Phone Number (JAVA题解)
- 17. Letter Combinations of a Phone Number LeetCode题解
- Leetcode题解-17. Letter Combinations of a Phone Number
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- 再多脖也是个糊涂脑子...
- 中国敢划防空识别圈就已经将美国的因素考虑...
- 本人的Linux服务器环境(LAMP架构)搭建过程
- Program v/s Process v/s Thread
- 转-基于Eclipse的Hadoop应用开发环境配置
- LeetCode题解:Combinations
- Hadoop完全分布式配置
- 深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing) 第二节 栈基本工作原理
- 句柄表
- Merge Two Sorted Lists 合并两个有序链表@LeetCode
- 正则表达式的解析 简单例子 udacity学习
- C# Tips: 将 VS2012 / VS2013 的.sln文件、project文件转换成 VS2010格式
- 查看linux版本的三种常用方法
- Linux find常见用法