일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- pwnable.kr
- c언어
- 스택
- Stack
- 큐
- C
- 두근두근 자료구조
- System
- LoB
- 시간복잡도
- 정렬 알고리즘
- ftz
- 재귀
- 암호수학
- ftz level13
- PHP
- windosw 문자열
- level13
- 백준
- 자료구조
- web
- SWiFT
- HTML
- 미로 탐색 알고리즘
- Java
- windosws wbcs
- OSI
- 파이썬
- War Game
- 파일 시스템
Archives
- Today
- Total
나의 기록, 현진록
[Swift] 프로그래머스 타겟넘버 본문
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/43165
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
주어진 배열에 있는 숫자들로 더하거나 빼서 targetNumber를 만들기
- 주어진 배열에 있는 숫자들을 더하거나 뺀 값으로 재귀호출을 반복(DFS)
- 재귀호출이 끝났을 경우 targetNumber와 같을 경우 count + 1
- 모든 DFS 종료 시 count 출력
import Foundation
func solution(_ numbers:[Int], _ target:Int) -> Int {
func dfs(dept: Int, result: Int){
if numbers.count-1 < dept{
if result == target{
count += 1
}
return
}
dfs(dept: dept+1, result: result - numbers[dept])
dfs(dept: dept+1, result: result + numbers[dept])
}
var count = 0
dfs(dept: 0, result: 0)
return count
}
반응형
'Programming > Algorithm & Data Structure' 카테고리의 다른 글
[Swift] 백준 치킨 배달 15686 (0) | 2025.03.25 |
---|---|
[Swift] 프로그래머스 이중우선순위큐 (0) | 2025.03.24 |
[Swift] 프로그래머스 여행경로 (0) | 2025.03.12 |
[Swift] 백준 1022 소용돌이 예쁘게 출력하기 (0) | 2025.03.10 |
[Swift] 프로그래머스 Lv3 상담원 민원 (0) | 2025.03.03 |