Archive/Questions
[programmers/Swift] 없는 숫자 더하기
Marco
2022. 5. 8. 19:40
728x90
programmers.co.kr - 코딩테스트연습 - Lv.1 - 월간 코드 챌린지 시즌 2 - 없는 숫자 더하기
코딩테스트 연습 - 없는 숫자 더하기
0부터 9까지의 숫자 중 일부가 들어있는 정수 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요.
programmers.co.kr
Constraints :
- 1 ≤ numbers의 길이 ≤ 9
- 0 ≤ numbers의 모든 원소 ≤ 9
- numbers의 모든 원소는 서로 다릅니다.
//
// Created by Yongwoo Marco on 2022/05/05.
// Copyright © 2022 Yongwoo Marco Kim. All rights reserved.
//
func solution(_ numbers:[Int]) -> Int {
return (1...9).reduce(0) { numbers.contains($1) ? $0 : $0 + $1 }
}
print(solution([5,8,4,0,6,7,9]))
print(solution([1,2,3,4,6,7,8,0]))
How I tried this :
1...9 로 정해진 수가 있기 때문에 배열에 들어있는지만 판별해서 구했다.
문제에 관한 모든 저작권 : https://programmers.co.kr/
728x90