Lewis's Tech Keep
[프로그래머스] 타겟 넘버 본문
기본적인 재귀, backtracking에 대한 문제
로직상 트리처럼 내려가면서 진행하는데 이를 어떻게 처리하고 결과값을 낼 지가 중요
더보기
class Solution {
int[] numbers;
int target;
int count = 0;
private void rec(int index, int cSum) {
if(numbers.length == index) {
if(cSum == target) {
count++;
}
return;
}
rec(index+1, cSum + numbers[index]);
rec(index+1, cSum - numbers[index]);
}
public int solution(int[] numbers, int target) {
this.numbers = numbers;
this.target = target;
rec(0, 0);
return count;
}
}
'Java > 알고리즘' 카테고리의 다른 글
[프로그래머스] 네트워크 (0) | 2021.01.30 |
---|---|
[프로그래머스] 이중우선순위 (0) | 2021.01.29 |
[프로그래머스] 오답 - 디스크 컨트롤러 (0) | 2021.01.27 |
[프로그래머스] 기능개발 (0) | 2021.01.22 |
[프로그래머스] 주식가격 (0) | 2021.01.21 |
Comments