-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
💬 문제
[코딩테스트 연습 - 나누어 떨어지는 숫자 배열](https://school.programmers.co.kr/learn/courses/30/lessons/12910)
💬 Idea
- 답이 오름차순으로 정렬되어야하므로 arr를 정렬한 뒤 for문을 돌아 i가 divisor로 나누어떨어진다면
- answer 배열에 i를 append한다.
- answer배열이 비어있다면 [-1]을 리턴하고, 그렇지 않다면 answer를 리턴한다.
💬 풀이
func solution(_ arr:[Int], _ divisor:Int) -> [Int] {
var answer: [Int] = []
for i in arr.sorted() {
if i % divisor == 0 {
answer.append(i)
}
}
return answer.isEmpty ? [-1] : answer
}
💬 더 나은 방법?
func solution(_ arr:[Int], _ divisor:Int) -> [Int] {
let array = arr.sorted().filter{ $0 % divisor == 0 }
return array == [] ? [-1] : array
}