프로그래머스 알고리즘 문제 기록/코딩 기초 트레이닝
배열 만들기 1
Lust3r
2024. 1. 22. 17:37
728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/181901
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
내 답안 / 다른 사람들의 답안
import Foundation
func solution(_ n:Int, _ k:Int) -> [Int] {
return (1...n).filter { $0 % k == 0 }.sorted()
}
- 1 ~ n의 숫자 배열에서 filter를 통해 k의 배수인 배열을 가져오고, 정렬하여 반환
import Foundation
func solution(_ n:Int, _ k:Int) -> [Int] {
return stride(from: k, through: n, by: k).map { $0 }
}
https://developer.apple.com/documentation/swift/stride(from:through:by:)
stride(from:through:by:) | Apple Developer Documentation
Returns a sequence from a starting value toward, and possibly including, an end value, stepping by the specified amount.
developer.apple.com
- from부터 through까지 by만큼 증가시키는 메서드(from, to, by는 to를 포함시키지 않고, through는 포함)