Post

[Programmers] #181889 - n 번째 원소까지 [Java][C++][Python]

[Programmers] #181889 - n 번째 원소까지 [Java][C++][Python]

문제 링크


1. 아이디어

$n$번째 원소까지는 앞에서 $n$개이고, 인덱스로는 0부터 $n - 1$까지이다. 끝을 포함하지 않는 구간으로 자르면 끝이 $n$이 된다.


2. 코드

풀이 [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.copyOf(num_list, n);
    }
}
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(), num_list.begin() + n);
}
1
2
def solution(num_list, n):
    return num_list[:n]

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