Post

[Programmers] #181892 - n 번째 원소부터 [Java][C++][Python]

정수 리스트와 정수 n을 받아 n 번째 원소부터 마지막 원소까지의 리스트를 반환하는 워밍업 문제.

[Programmers] #181892 - n 번째 원소부터 [Java][C++][Python]

문제 링크


1. 아이디어

1부터 세는 n 번째 원소는 0-indexed로 n - 1 위치이므로, num_listn - 1부터 끝까지 잘라내면 된다.


2. 복잡도

접근시간공간
풀이$O(N)$$O(N)$

($N$ = num_list의 길이)


3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
import java.util.*;

class Solution {
    public int[] solution(int[] num_list, int n) {
        return Arrays.copyOfRange(num_list, n - 1, num_list.length);
    }
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

vector<int> solution(vector<int> num_list, int n) {
    return vector<int>(num_list.begin() + n - 1, num_list.end());
}
1
2
def solution(num_list, n):
    return num_list[n - 1 :]

This post is licensed under CC BY 4.0 by the author.