[Programmers] #181889 - n 번째 원소까지 [Java][C++][Python]
정수 리스트의 첫 원소부터 n 번째 원소까지를 담은 리스트를 구하는 워밍업 문제.
[Programmers] #181889 - n 번째 원소까지 [Java][C++][Python]
1. 아이디어
정수 리스트 num_list의 첫 원소부터 n번째 원소까지, 즉 앞에서부터 n개의 원소를 잘라낸 부분 리스트가 곧 답이다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = 잘라낼 길이, 파라미터 n)
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.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.