[Programmers] #120821 - 배열 뒤집기 [Java][C++][Python]
배열의 원소 순서를 뒤집어 반환하는 워밍업 문제.
[Programmers] #120821 - 배열 뒤집기 [Java][C++][Python]
1. 아이디어
num_list의 원소 순서를 반대로 뒤집어 새 배열로 반환하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = num_list의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int[] solution(int[] num_list) {
int n = num_list.length;
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = num_list[n - 1 - i];
}
return ans;
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> num_list) {
return vector<int>(num_list.rbegin(), num_list.rend());
}
역방향 이터레이터를 vector 생성자에 넘겨 뒤집힌 벡터를 새로 만들었다.
1
2
def solution(num_list):
return num_list[::-1]
This post is licensed under CC BY 4.0 by the author.