- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargetNumber.cpp
More file actions
Latest commit
22 lines (19 loc) · 480 Bytes
/
Copy pathtargetNumber.cpp
File metadata and controls
22 lines (19 loc) · 480 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<string>
#include<vector>
usingnamespacestd;
voiddfs(vector<int> numbers, int target, int& answer, int count = 0, int sum = 0) {
if (count == numbers.size()) {
if (sum == target)
answer++;
return;
}
else {
dfs(numbers, target, answer, count + 1, sum + numbers[count]);
dfs(numbers, target, answer, count + 1, sum - numbers[count]);
}
}
intsolution(vector<int> numbers, int target) {
int answer = 0;
dfs(numbers, target, answer);
return answer;
}