728x90
[Swift] 프로그래머스 타겟 넘버(lv. 2)
1. 문제
https://school.programmers.co.kr/learn/courses/30/lessons/43165
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
2. 접근
3. 코드
import Foundation
func solution(_ numbers:[Int], _ target:Int) -> Int {
var count = 0
func dfs(_ index: Int, _ sum: Int) {
if index == numbers.count {
if sum == target {
count += 1
}
return
}
dfs(index + 1, sum + numbers[index])
dfs(index + 1, sum - numbers[index])
}
dfs(0, 0)
return count
}
728x90
'Problem Solving' 카테고리의 다른 글
[Swift] 프로그래머스 [3차] 압축(lv. 2) (0) | 2023.04.04 |
---|---|
[Swift] 프로그래머스 연속 부분 수열 합의 개수(lv. 2) (0) | 2023.04.03 |
[Swift] 프로그래머스 k진수에서 소수 개수 구하기(lv. 2) (0) | 2023.03.31 |
[Swift] 프로그래머스 귤 고르기(lv. 2) (0) | 2023.03.29 |
[Swift] 프로그래머스 [1차] 뉴스 클러스터링(lv. 2) (0) | 2023.03.28 |
댓글