Title
Given-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], [up], [1,3], [1,4],]
Link to original topic (point me)
Thinking of solving problems
Combinatorial problem, the old idea---recursive plus cycle, this is the combination of simple.
Code implementation
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.s Ize ()) { 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 ();}} ;
If you think this article has a harvest for you, please help the top.
In addition, I opened the public number- sharing the beauty of technology , I will not regularly share some of my learning things.You can search the public number:
Swalgeor scan the QR code below to follow me .
(Reprint article Please specify Source: http://blog.csdn.net/swagle/article/details/30761165)
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
[Leetcode] combinations [38]