forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
24 lines (24 loc) · 631 Bytes
/
Copy paths1.cpp
File metadata and controls
24 lines (24 loc) · 631 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// OJ: https://leetcode.com/problems/combinations/
// Author: github.com/lzl124631x
// Time: O(K!)
// Space: O(K)
class Solution {
vector<vector<int>> ans;
void dfs(int n, int k, int start, vector<int> &tmp) {
if (tmp.size() == k) {
ans.push_back(tmp);
return;
}
for (int i = start, end = n - k + tmp.size(); i <= end; ++i) {
tmp.push_back(i + 1);
dfs(n, k, i + 1, tmp);
tmp.pop_back();
}
}
public:
vector<vector<int>> combine(int n, int k) {
vector<int> tmp;
dfs(n, k, 0, tmp);
return ans;
}
};