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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <vector>
 
using namespace std;
 
vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) 
{
    vector<vector<int>> answer(arr1.size(), vector<int>(arr1[0].size(), 0));
    
    for(int i = 0 ; i < arr1.size() ; i++)
    {
        for(int j = 0 ; j < arr1[0].size() ; j++)
        {
            answer[i][j] = arr1[i][j] + arr2[i][j];
        }
    }
    
    return answer;
}
 

그냥 행렬의 덧셈이다.

벡터를 생성과 동시에 초기화하면 capacity가 변하지 않는다.

 

fill constructor (벡터 생성자 초기화)

각 원소에 할당된 값이 있는 n개의 원소를 가진 컨테이너를 생성합니다.

 

시간 복잡도

O(n)

 

선언

vector<자료형> 컨테이너이름(n, value);

n 컨테이너의 크기

value 컨테이너의 각 원소에 할당될 값



출처: https://woodforest.tistory.com/206 [나무 숲]

+ Recent posts