알고리즘/프로그래머스
타겟넘버 (자바)
김어찐
2021. 9. 8. 16:51
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