-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path77.cpp
55 lines (51 loc) · 1.36 KB
/
77.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
__________________________________________________________________________________________________
60ms
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
dfs(1,n,k);
return res;
}
private:
void dfs(int index,int n,int k){
if(k==0){
res.push_back(oneres);
return;
}
if(index==n+1) return;
for(int i=index;i<=n-k+1;++i){
oneres.push_back(i);
dfs(i+1,n,k-1);
oneres.pop_back();
}
}
vector<vector<int>> res;
vector<int> oneres;
};
__________________________________________________________________________________________________
11564 kb
class Solution {
public:
vector<vector<int>> combine(int n, int k)
{
vector<vector<int>> sol;
vector<int> sol1(k, 0);
int i = 0;
while (i>=0) {
sol1[i]++;
//left position, i+1, i+2,..., k-1 (total count k-i-1)
//left numbers, s[i]+1, s[i]+2, ..., n (total n-s[i])
//if left number<left position, no need to continue
if (sol1[i]>n-k+i+1 )
--i;
else if (i==k-1)
sol.push_back(sol1);
else {
++i;
sol1[i] = sol1[i-1];
}
}
return sol;
}
};
__________________________________________________________________________________________________