본문 바로가기
알고리즘/프로그래머스

타겟넘버 (자바)

by 김어찐 2021. 9. 8.
728x90
package prog;

public class 타겟넘버 {
    public static void main(String[] args) {
        Solution_타겟넘버 s = new Solution_타겟넘버();
        System.out.println(s.solution(new int[] {1, 1, 1, 1, 1},3));
    }

}
class Solution_타겟넘버{
    static int TG;
    static int answer;
    public int solution(int[] numbers, int target) {

        TG = target;
        dfs(numbers,0,0);
        return answer;
    }

    private void dfs(int[] numbers, int cnt, int now) {
        if(cnt == numbers.length){
            if (now==TG){
                answer++;
            }
            return;
        }
        dfs(numbers,cnt+1,now+numbers[cnt]);
        dfs(numbers,cnt+1,now-numbers[cnt]);
    }


}
728x90

'알고리즘 > 프로그래머스' 카테고리의 다른 글

짝지어 제거하기 (자바)  (0) 2021.09.09
더 맵게 (자바)  (0) 2021.09.08
기능개발 (자바)  (0) 2021.09.07
124 나라의 숫자 (자바)  (0) 2021.09.06
멀쩡한 사각형 (자바)  (0) 2021.09.06