static int answer = 0;
public static int solution(int[] numbers, int target) {
answer = dfs(numbers, 0, target, 0);
return answer;
}
public static int dfs(int[] numbers, int depth, int target, int sum) {
if (depth == numbers.length) {
if (target == sum)
return 1;
return 0;
}
return dfs(numbers, depth + 1, target, sum + numbers[depth])
+ dfs(numbers, depth + 1, target, sum - numbers[depth]);
}
전형적인 DFS문제