코린이의 소소한 공부노트

[프로그래머스/Lv.1] 행렬의 덧셈 본문

코딩테스트 풀이/JAVA

[프로그래머스/Lv.1] 행렬의 덧셈

무지맘 2022. 11. 22. 14:59

1. Input

1) int 행렬 arr1

2) int 행렬 arr2

 

2. Output

1) arr1arr2의 덧셈 결과

 

3. Constraint

1) 두 행렬의 행과 열의 길이는 500을 넘지 않는다.

2) 두 행렬의 크기는 같다.

 

4. Example

Input: arr1={{1,2},{2,3}}, arr2={{3,4},{5,6}} -> Output: {{4,6}.{7,9}}

설명:

{ {1+3, 2+4}, = { {4, 6},

  {2+5, 3+6}}      {7, 9}}

 

5. Code

1) 첫 코드(2022/??)

int row = arr1.length;
int col = arr1[0].length;
int[][] answer = new int[row][col];

for(int i=0 ; i<row ; i++){
    for(int j=0 ; j<col ; j++)
        answer[i][j] = arr1[i][j] + arr2[i][j];
}

return answer;