본문 바로가기

Algorithm/Programers - C++

[프로그래머스] 행렬의 덧셈

문제

 

풀이

#include <string>
#include <vector>

using namespace std;

vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) {
    vector<vector<int>> answer;
    for(int i=0; i<arr1.size(); i++){
        vector<int> vec;
        answer.push_back(vec);
        for(int j=0; j<arr1[i].size(); j++)
            answer[i].push_back(arr1[i][j]+arr2[i][j]);
    }
    return answer;
}

 

 

 

 


 

다른 사람 코드

#include<iostream>
#include<vector>
using namespace std;

vector<vector<int> > sumMatrix(vector<vector<int> >A, vector<vector<int> >B)
{
    vector<vector<int> > answer = A;
    for (int i = 0; i < answer.size(); i++)
    {
        for (int j = 0; j<answer[0].size(); j++)
        {
            answer[i][j] = A[i][j] + B[i][j];
        }
    }
    return answer;
}

vector <vector <int> > answer = A;

이런 방법이!