class Solution {
static int answer=0;
public int solution(int[] numbers, int target) {
int sum = 0;
dfs(numbers, target, sum, 0);
return answer;
}
static void dfs(int[] numbers, int target, int sum, int depth) {
//base case
if (depth == numbers.length) {
if (sum == target) {
answer ++;
}
} else {
//rercursive case
dfs(numbers, target, sum + numbers[depth], depth+1);
dfs(numbers, target, sum - numbers[depth], depth+1);
}
}
}
'알고리즘' 카테고리의 다른 글
조합론 이론과 구현 (0) | 2022.07.24 |
---|---|
프로그래머스-HashMap사용 : 위장 (getOfDefault) (0) | 2022.07.02 |
[백준-그래프탐색-dfs] 1520 - 내리막길 (0) | 2022.07.02 |
[백준1260] DFS와 BFS (0) | 2022.07.02 |
자질구레한 알고리즘 모음 (0) | 2022.05.13 |