본문 바로가기
프로그래머스/[프로그래머스 - JAVA] Lv.0

[프로그래머스 - JAVA] 2차원으로 만들기

by 코딩하는 흰둥이 2023. 3. 11.
반응형

 

  • 내 풀이
import java.util.Arrays;
import java.util.ArrayList;
import java.util.stream.Collectors;

class Solution {
    public int[][] solution(int[] num_list, int n) {
        int[][] answer = {};

        int t = num_list.length / n;

        answer = new int[t][n];

        ArrayList<Integer> arrays = (ArrayList<Integer>) Arrays.stream(num_list).boxed().collect(Collectors.toList());

        for (int i = 0; i < t; i++) {
            for (int j = 0; j < n; j++) {
                answer[i][j] =arrays.get(j);
            }
            for (int j = 0; j < n; j++) {
                arrays.remove(0);
            }
        }
        return answer;
    }
}

이 시간쯤 되면 머리가 안돌아가는게 확실한거 같다

num_list에서 값을 가져올 count 증가 변수를 만들면 ArrayList로 굳이 remove를 하지 않아도 되고 불필요한 for문을 쓸 일도 없을텐데 멍..하다보니 되려 돌아도 너무 돌아간거같다....

 

  • 다른 사람 풀이
class Solution {
    public int[][] solution(int[] num_list, int n) {
        int[][] answer = new int[num_list.length/n][n];
        int cnt = 0;
        for(int i = 0 ; i < num_list.length/n ; i++){
            for(int j = 0 ; j < n ; j++){
                answer[i][j] = num_list[cnt];
                cnt++;
            }
        }
        return answer;
    }
}

 

댓글