반응형

 

출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

 

 

 

** Javascript

 

 

function solution(arr1, arr2) {
    let answer = []
    for (let i = 0; i < arr1.length; i++) {
        let arr = [];
        for (let j = 0; j < arr2[0].length; j++) {
            let sum = 0
            for (let k = 0; k < arr2.length; k++) {
                sum += arr1[i][k] * arr2[k][j];
            }
            arr.push(sum);
        }
        answer.push(arr);
    }
    return answer;
}

 

 

 

** Python (+3)

 

def solution(arr1, arr2):
    answer = []
    for i in range(len(arr1)):
        _list = []
        for j in range(len(arr2[0])):
            _sum = 0
            for k in range(len(arr2)):
                _sum += arr1[i][k] * arr2[k][j]
            _list.append(_sum)
        answer.append(_list) 
        
    return answer

 

 

** Java

 

class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        int[][] answer = new int[arr1.length][arr2[0].length];
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2[0].length; j++) {
                for (int k = 0; k < arr2.length; k++) {
                    answer[i][j] += arr1[i][k] * arr2[k][j];
                }
            }
        }
        return answer;
    }
}

 

반응형

+ Recent posts