KWiOS
KWiOS0101
KWiOS
  • 분류 전체보기 (108)
    • Algorithm (41)
      • 이코테 (14)
      • 이코테 문제풀이 (21)
      • 프로그래머스 (6)
    • CS (1)
      • 모두를 위한 컴퓨터 과학(CS50 2019) (0)
    • iOS (15)
    • Swift (36)
      • Swift문법 (32)
      • 기타 (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 6

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
KWiOS

KWiOS0101

Algorithm/프로그래머스

Level2 - 타겟넘버(Swift)

2022. 8. 9. 12:50
var numbersCP = [Int]()
var targetCP = 0
var result = 0

func solution(_ numbers:[Int], _ target:Int) -> Int {
    numbersCP = numbers
    targetCP = target 
    
    dfs(0,0)
    
    return result
}


func dfs(_ sum: Int, _ depth: Int) { 
    
    if depth == numbersCP.count {
        if sum == targetCP {
            result += 1
        }
        return 
    }
    
    dfs(sum + numbersCP[depth], depth + 1)
    dfs(sum - numbersCP[depth], depth + 1)
    
}

'Algorithm > 프로그래머스' 카테고리의 다른 글

Level2 - H - Index(Swift)  (0) 2022.08.09
Level2 - 가장 큰 수(Swift)  (0) 2022.08.09
Level1 - 신규 아이디 추천(Swift)  (0) 2022.08.07
Level1 - 로또의 최고 순위와 최저 순위(Swift)  (0) 2022.07.31
Level1 - 신고 결과 받기(Swift)  (0) 2022.07.24
    'Algorithm/프로그래머스' 카테고리의 다른 글
    • Level2 - H - Index(Swift)
    • Level2 - 가장 큰 수(Swift)
    • Level1 - 신규 아이디 추천(Swift)
    • Level1 - 로또의 최고 순위와 최저 순위(Swift)
    KWiOS
    KWiOS

    티스토리툴바