Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7cb0ec3

Browse files
authored
Merge pull requestneetcode-gh#3351 from tapasyadimree/add-leetcode-solution
Create: 0077-combinations.cpp
2 parents9ae6534 +4b4d1f2 commit7cb0ec3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎cpp/0077-combinations.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
private:
3+
voidbacktrack(int start,int n,int k, vector<int> &combination, vector<vector<int>> &res){
4+
//base case, when size of combination is k, we wanna stop
5+
if(combination.size() == k){
6+
res.push_back(combination);
7+
return;
8+
}
9+
10+
for(int i = start; i<=n; i++){
11+
combination.push_back(i);
12+
backtrack(i+1, n, k, combination, res);
13+
combination.pop_back();
14+
}
15+
}
16+
public:
17+
vector<vector<int>>combine(int n,int k) {
18+
vector<vector<int>> res;
19+
20+
//initial empty list to pass to the backtrack function
21+
vector<int> emptyCombination;
22+
23+
backtrack(1, n, k, emptyCombination, res);
24+
25+
return res;
26+
}
27+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp