+
, -
한 결과를 모두 저장 후 target 개수를 세어 반환import java.util.ArrayList;
class Solution {
ArrayList<Integer> nums = new ArrayList<>();
public int solution(int[] numbers, int target) {
getNumber(numbers, numbers.length, 0, 0);
return (int) nums.stream().filter(num -> num == target).count();
}
private void getNumber(int[] numbers, int size, int count, int num) {
if (size == count) nums.add(num);
if (count < size) {
getNumber(numbers, size, count + 1, num + numbers[count]);
getNumber(numbers, size, count + 1, num - numbers[count]);
}
}
}