Skip to content

Latest commit

 

History

History
29 lines (25 loc) · 760 Bytes

File metadata and controls

29 lines (25 loc) · 760 Bytes

프로그래머스 Level2 : 깊이/너비 우선 탐색(DFS/BFS) 타겟 넘버

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