-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCombinations
More file actions
42 lines (38 loc) · 865 Bytes
/
Copy pathCombinations
File metadata and controls
42 lines (38 loc) · 865 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution
{
public:
vector<vector<int>> combine(int n, int k)
{
vector<int> one;
for (int i = n - k + 1; i <= n; ++i)
{
one.push_back(i);
}
vector<vector<int>> all({one});
while (true)
{
int i = k - 1;
for (; i > 0; --i)
{
if (one[i] > one[i-1] + 1)
{
break;
}
}
if (i > 0 || one[i] > 1)
{
one[i] -= 1;
for (int j = k - 1; j > i; --j)
{
one[j] = n + 1 - k + j;
}
}
else
{
break;
}
all.push_back(one);
}
return all;
}
};