[LeetCode] Combinations [38]
2014-06-14 17:46
267 查看
题目
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: vector<vector<int> > combine(int n, int k) { vector<vector<int> > ret; helper(k, 1, n, vector<int>(), ret); return ret; } void helper(int k, int start, int n, vector<int> part, vector<vector<int> > &ret){ if(k==part.size()){ ret.push_back(part); return; } for(int i=start; i<=n; ++i){ part.push_back(i); helper(k, i+1, n, part, ret); part.pop_back(); } } };
如果你觉得本篇对你有收获,请帮顶。
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你可以搜索公众号:swalge 或者扫描下方二维码关注我
(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30761165 )
相关文章推荐
- [LeetCode] Combinations [38]
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode : Letter Combinations of a Phone Number [java]
- [leetcode]Letter Combinations of a Phone Number
- LeetCode-77 Combinations(组合)
- LeetCode-17. Letter Combinations of a Phone Number
- Leetcode38: Rectangle Area
- [LeetCode]38 Count and Say
- LeetCode (17)Letter Combinations of a Phone Number
- Combinations -- LeetCode
- LeetCode-Combinations
- leetcode Letter Combinations of a Phone Number python
- [leetcode 17] Letter Combinations of a Phone Number
- [leetcode][回溯] Combinations
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-38 count and say
- LeetCode18:Letter Combinations of a Phone Number
- [LeetCode 38] Count and Say
- [Leetcode]Combinations & Permutations