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

0

[문제링크 - 프로그래머스 - 행렬의 덧셈] https://school.programmers.co.kr/learn/courses/30/lessons/12950

나의 풀이

class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        int[][] answer = new int[arr1.length][arr1[0].length];

        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;
    }
}



다른사람 풀이

  • answer 초기값을 다른 배열을 선언하여 지정할 수 도 있다는 것을 알게되었다.
  • 그리고 answer = arr1 + arr2 형식이 아닌 answer += arr2 로 구현가능 하다는 것을 알게되었다.
class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        int[][] answer = {};
        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;
    }
}
profile
초심 잃지 않기

0개의 댓글

관련 채용 정보