FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/Backtracking/Combination_sum_2.cpp at master · kothariji/competitive-programming · GitHub
kothariji
/
competitive-programming
Public
Notifications
You must be signed in to change notification settings
Fork
500
Star
704
Code
Issues
1
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
competitive-programming
/
Backtracking
/
Combination_sum_2.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
28 lines (23 loc) · 772 Bytes
Breadcrumbs
competitive-programming
/
Backtracking
/
Combination_sum_2.cpp
Copy path
File metadata and controls
28 lines (23 loc) · 772 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class
Solution
{
public:
void
dfs
(
int
ind,
int
t, vector<
int
> v, vector<
int
> &ar, vector<vector<
int
>> &ans){
if
(t <
0
)
return
;
if
(t ==
0
){
ans.
push_back
(v);
return
;
}
if
(ind >= ar.
size
())
return
;
for
(
int
i = ind; i < ar.
size
(); i++){
if
((i != ind && ar[i] == ar[i-
1
] ) || ar[i] > t)
continue
;
v.
push_back
(ar[i]);
dfs
(i +
1
, t - ar[i], v, ar, ans);
v.
pop_back
();
}
}
vector<vector<
int
>>
combinationSum2
(vector<
int
>& candidates,
int
target) {
vector<vector<
int
>> ans;
sort
(candidates.
begin
(), candidates.
end
());
dfs
(
0
, target, {}, candidates, ans);
return
ans;
}
};
Back
|
FazBrowse Home
|
New Git URL