Jam's story
[프로그래머스] 행렬의 덧셈 본문
- 답을 반환할 배열을 크기대로 선언
- for문을 돌려서 행렬을 더해준다.
- 답반환
class Solution {
public int[][] solution(int[][] arr1, int[][] arr2) {
int len1=arr1.length;
int len2=arr1[0].length;
int[][] answer = new int[len1][len2];
for(int i=0; i<arr1.length; i++){
for(int j=0; j<arr1[0].length; j++){
answer[i][j]=arr1[i][j]+arr2[i][j];
}
}
return answer;
}
}
다른 사람풀이
- arr1과 arr2 크기는 같고, 반환할 배열 크기도 같으니 answer 배열을 arr1배열로
- for문을 돌려서 answer 배열에 값을 더한값으로 바꿔준다.
class Solution {
public int[][] solution(int[][] arr1, int[][] arr2) {
int[][] answer = arr1;
for(int i=0; i<arr1.length; i++){
for(int j=0; j<arr1[0].length; j++){
answer[i][j] += arr2[i][j];
}
}
return answer;
}
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 다음에 올 숫자 - java (0) | 2022.12.09 |
---|---|
[프로그래머스] n의 배수 고르기 (0) | 2022.11.20 |
[프로그래머스] 휴대폰 번호 가리기 (0) | 2022.08.13 |
[프로그래머스] 콜라츠 추측 (0) | 2022.08.12 |
[프로그래머스] 최대공약수와 최소공배수 (0) | 2022.08.10 |