[Leetcode] combinations [38]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.