Lewis's Tech Keep

[프로그래머스] 타겟 넘버 본문

Java/알고리즘

[프로그래머스] 타겟 넘버

Lewis Seo 2021. 1. 28. 02:42

기본적인 재귀, 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;
    }
}
Comments