[Programmers] 타겟 넘버

Date:

[Programmers] 타겟 넘버

Problem URL : 타겟 넘버

#include <string>
#include <vector>

using namespace std;
int n;
int answer;
int t;
void dfs(vector<int> &numbers, int index, int sum) {
    if (index == n) {
        if(sum == t){
            answer++;
        }
        return;
    }
    dfs(numbers, index + 1, sum + numbers[index]);
    dfs(numbers, index + 1, sum - numbers[index]);
}

int solution(vector<int> numbers, int target) {
    n = numbers.size();
    t = target;
    answer = 0;
    dfs(numbers, 0, 0);
    return answer;
}

댓글