코테 공부

[DFS]타겟 넘버(프로그래머스, 자바)

DaEun_ 2022. 11. 8. 19:13

코딩테스트 연습 - 타겟 넘버 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

class Solution {
    
    static int answer = 0;
    public int solution(int[] numbers, int target) {
        
        dfs(numbers,target,0,0);
        return answer;
    }
    
    static void dfs(int[] numbers,int target, int index, int sum){
        
        if(index==numbers.length){
            if(sum==target){
                answer++;
            }
            return;
        } 
        
     
        dfs(numbers,target,index+1, sum+numbers[index]);
        dfs(numbers,target,index+1, sum-numbers[index]);
    }
}