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

LeetCode – Refresh – Combinations

时间:2015-03-19 06:20:58      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Same algorithm with combination. Input as the (1..n), constraint is that the length of each combine should be k.

 1 class Solution {
 2 public:
 3     void getComb(vector<vector<int> > &result, vector<int> current, int n, int k, int start) {
 4         if (current.size() == k) {
 5             result.push_back(current);
 6             return;
 7         }
 8         for (int i = start; i <= n; i++) {
 9             current.push_back(i);
10             getComb(result, current, n, k, i+1);
11             current.pop_back();
12         }
13     }
14     vector<vector<int> > combine(int n, int k) {
15         vector<vector<int> > result;
16         getComb(result, vector<int> (), n, k, 1);
17         return result;
18     }
19 };

 

LeetCode – Refresh – Combinations

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4349270.html

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