class Solution {
static int[] numbers;
static int N;
static int target;
static int count = 0;
public int solution(int[] numbers, int target) {
Solution.numbers = numbers;
N = numbers.length;
Solution.target = target;
dfs(0, 0);
return count;
}
static void dfs(int idx, int sum) {
if (idx == N) {
if (sum == target) {
count++;
}
return;
}
dfs(idx + 1, sum + numbers[idx]);
dfs(idx + 1, sum - numbers[idx]);
}
}
static 변수를 두고 푸는게 편하게 느껴진다.