2차원으로 만들기

han.user();·2023년 4월 2일
0

프로그래머스

목록 보기
30/87
post-thumbnail

import java.util.Arrays;

class Solution {
    public int[][] solution(int[] num_list, int n) {
        int m = num_list.length / n; // 열의 개수 계산

        int[][] answer = new int[m][n]; // m행 n열의 2차원 배열 생성
        int index = 0; // num_list에서 값을 가져오기 위한 인덱스 변수

        // 2차원 배열의 각 요소에 num_list의 값을 할당
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                answer[i][j] = num_list[index];
                index++;
            }
        }
        return answer;
    }
}
profile
I'm still hungry.

0개의 댓글