프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <vector>
 
using namespace std;
 
vector<long long> solution(int x, int n) {
    vector<long long> answer;
    answer.reserve(n);
    
    for(int i = 0 ; i < n ; i++)
        answer.emplace_back(x * (i + 1));
    
    return answer;
}
 

굳이 포스팅할 문제는 아니지만, vector를 미리 reserve() 하고 사용하면, 컨테이너용량이 늘어날때 벡터의 capacity가 재할당 되기때문에 이를 방지한다는 것을 알리려고 포스팅했다.

+ Recent posts