방명록
- [프로그래머스 / 자바] 2차원으로 만들기2023년 10월 23일 11시 24분 07초에 업로드 된 글입니다.작성자: 민발자728x90
문제
정수 배열 num_list와 정수 n이 매개변수로 주어집니다. num_list를 다음 설명과 같이 2차원 배열로 바꿔 return하도록 solution 함수를 완성해주세요. num_list가 [1, 2, 3, 4, 5, 6, 7, 8] 로 길이가 8이고 n이 2이므로 num_list를 2 * 4 배열로 다음과 같이 변경합니다. 2차원으로 바꿀 때에는 num_list의 원소들을 앞에서부터 n개씩 나눠 2차원 배열로 변경합니다
풀이
class Solution { public int[][] solution(int[] num_list, int n) { int[][] answer = new int[num_list.length/n][n]; int idx = 0; for(int i = 0; i < num_list.length / n; i++){ for(int j = 0; j < n; j++){ answer[i][j] = num_list[idx++]; } } return answer; } }
참고할만한 다른 사람 풀이
class Solution { public int[][] solution(int[] num_list, int n) { int[][] answer = {}; int length = num_list.length; answer = new int[length/n][n]; for(int i=0; i<length; i++){ answer[i/n][i%n]=num_list[i]; } return answer; } }
728x90'기록 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 / 자바] 합성수 구하기 (0) 2023.10.23 [프로그래머스 / 자바] 배열 회전시키기 (0) 2023.10.23 [프로그래머스] 모스부호(1) (1) 2023.10.20 [프로그래머스] 순서쌍 구하기 (1) 2023.10.20 [프로그래머스] 진료순서 정하기 (0) 2023.10.20 다음글이 없습니다.이전글이 없습니다.댓글