forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combinations.cpp
64 lines (54 loc) · 1.09 KB
/
Combinations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: June 26, 2012
Problem: Combinations
Difficulty: Easy
Source: http://www.leetcode.com/onlinejudge
Notes:
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],
]
Solution:
Pure recursion.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
vector<vector<int> > result;
int max;
int num;
void recursion(int index, vector<int> q) {
if (q.size() >= num) {
result.push_back(q);
return;
}
for (int i = index; i <= max; i++) {
q.push_back(i);
recursion(i + 1, q);
q.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
max = n;
num = k;
result.clear();
vector<int> q;
recursion(1, q);
return result;
}
};