-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations.cpp
More file actions
44 lines (42 loc) · 1.16 KB
/
Copy pathcombinations.cpp
File metadata and controls
44 lines (42 loc) · 1.16 KB
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
/**
* https://leetcode.com/problems/combinations/
* Backtracking
* Medium
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void backtrack(vector<vector<int> >& combinations, vector<int>& currentCombo, int start, int k, int n){
if (k == 0) {
combinations.push_back(currentCombo);
return;
}
for (int i = start; i <= n; i++) {
currentCombo.push_back(i);
backtrack(combinations, currentCombo, i + 1, k - 1, n);
currentCombo.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > combinations;
vector<int> currentCombination;
backtrack(combinations, currentCombination, 1, k, n);
return combinations;
}
};
int main() {
int n, k;
cout << "Input n and k:\n";
cin >> n >> k;
Solution solution;
vector<vector<int> > combinations = solution.combine(n, k);
cout << "Combinations:\n";
for (int i = 0; i < combinations.size(); i++) {
for (int j = 0; j < k; j++) {
cout << combinations[i][j] << ' ';
}
cout << endl;
}
}