Programmers 타겟 넘버

Solution

image

def solution(numbers, target):
    parent = [0]
    for n in numbers:
        child = []
        for p in parent:
            child.append(p + n)
            child.append(p - n)
        parent = child
    return parent.count(target)
  • 다른 사람 풀이
def solution(numbers, target):
    if not numbers and target == 0 :
        return 1
    elif not numbers:
        return 0
    else:
        return solution(numbers[1:], target-numbers[0]) + solution(numbers[1:], target+numbers[0])