프로그래머스 181892번 n 번째 원소부터 - Python, Java
문제 설명
정수 리스트 num_list와 정수 n이 주어질 때, n 번째 원소부터 마지막 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요.
제한 사항
- 2 ≤
num_list의 길이 ≤ 30 - 1 ≤
num_list의 원소 ≤ 9 - 1 ≤
n≤num_list의 길이
입출력 예시
| num_list | n | result |
|---|---|---|
| [2, 1, 6] | 3 | [6] |
| [5, 2, 1, 7, 5] | 2 | [2, 1, 7, 5] |
입출력 예 설명 1
- [2, 1, 6]의 세 번째 원소부터 마지막 원소까지의 모든 원소는 [6]입니다.
입출력 예 설명 2
- [5, 2, 1, 7, 5]의 두 번째 원소부터 마지막 원소까지의 모든 원소는 [2, 1, 7, 5]입니다.
코드 구현
Python
def solution(num_list, n):
return num_list[n-1::]
Java
class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer = new int[num_list.length - n + 1];
for (int i = 0; i < answer.length; i += 1) {
answer[i] = num_list[i + n - 1];
}
return answer;
}
}