[Programmers] #120809 - 배열 두 배 만들기 [Java][C++][Python]
정수 배열 numbers의 각 원소를 두 배로 만들어 반환하는 워밍업 문제.
[Programmers] #120809 - 배열 두 배 만들기 [Java][C++][Python]
1. 아이디어
정수 배열 numbers가 주어질 때, 각 원소를 두 배로 만든 배열을 반환하면 되는 간단한 문제다. 배열을 순회하며 각 원소에 2를 곱하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = numbers 배열의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
class Solution {
public int[] solution(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
numbers[i] *= 2;
}
return numbers;
}
}
1
2
3
4
5
6
7
8
9
10
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> numbers) {
for (int& x : numbers) {
x *= 2;
}
return numbers;
}
1
2
def solution(numbers):
return [x * 2 for x in numbers]
리스트 컴프리헨션으로 새 리스트를 만들어 반환해줬다.
This post is licensed under CC BY 4.0 by the author.