Where who wants to meet someone

n 번째 원소부터 본문

프로그래머스 알고리즘 문제 기록/코딩 기초 트레이닝

n 번째 원소부터

Lust3r 2024. 1. 23. 13:54
728x90

문제

https://school.programmers.co.kr/learn/courses/30/lessons/181892

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내 답안 / 다른 사람들의 답안

import Foundation

func solution(_ num_list:[Int], _ n:Int) -> [Int] {
    return Array(num_list[(n - 1)...])
}

- n번째 원소는 인덱스로 보면 n-1이므로 num_list의 n - 1 부터의 범위에 해당하는 값을 Array로 반환

 

import Foundation

func solution(_ num_list:[Int], _ n:Int) -> [Int] {
    return num_list.suffix(num_list.count - n + 1)
}

https://developer.apple.com/documentation/swift/string/suffix(_:)

 

suffix(_:) | Apple Developer Documentation

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

developer.apple.com

 

점수: +1

'프로그래머스 알고리즘 문제 기록 > 코딩 기초 트레이닝' 카테고리의 다른 글

왼쪽 오른쪽  (1) 2024.01.24
순서 바꾸기  (0) 2024.01.23
배열 조각하기  (0) 2024.01.23
2의 영역  (0) 2024.01.23
배열 만들기 3  (0) 2024.01.23