码迷,mamicode.com
首页 > 其他好文 > 详细

77. Combinations

时间:2018-04-02 15:12:02      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:push   来源   cto   problems   tco   ref   ...   cpp   markdown   

77. Combinations

题目

 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_77 {
public:

    void help(vector<vector<int>>& vecs, vector<int> &vec,int i,int k,int n )
    {
        if (k==0)
        {
            vecs.push_back(vec);
            return;
        }
        if (i>n)
        {
            return;
        }

        vec.push_back(i + 1);
        help(vecs, vec, i + 1, k-1, n);
        vec.pop_back();
        help(vecs, vec, i + 1, k, n);

        return;

    }

    vector<vector<int>> combine(int n, int k) {

        vector<vector<int>> vecs;
        vector<int> vec;

        help(vecs,vec,0,k,n);

        return vecs;  
    }
};

题目来源

77. Combinations

标签:push   来源   cto   problems   tco   ref   ...   cpp   markdown   

原文地址:https://www.cnblogs.com/ranjiewen/p/8693126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!