[Programmers] #120833 - 배열 자르기 [Java][C++][Python]
[Programmers] #120833 - 배열 자르기 [Java][C++][Python]
1. 아이디어
정수 배열 numbers에서 인덱스 num1부터 num2까지의 구간을 그대로 잘라 반환하면 되는 간단한 문제다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = num2 - num1 + 1)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
import java.util.*;
class Solution {
public int[] solution(int[] numbers, int num1, int num2) {
return Arrays.copyOfRange(numbers, num1, num2 + 1);
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> numbers, int num1, int num2) {
return vector<int>(numbers.begin() + num1, numbers.begin() + num2 + 1);
}
1
2
def solution(numbers, num1, num2):
return numbers[num1 : num2 + 1]
This post is licensed under CC BY 4.0 by the author.