다른 사람의 풀이1
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;
}
}
▶ i/n을 하는 경우 0,1인덱스는 0행, 2, 3인덱스는 1행, 4,5인덱스는 2행, 6,7인덱스는 3행......반복
▶ i % n은 열을 의미한다.
다른 사람의 풀이2
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;
}
}
▶ 이중 for문을 통해서 2차원 배열에 값을 대입한다.
'프로그래머스(자바) > LV.0(자바)' 카테고리의 다른 글
가까운 수 → "특이한 정렬"과 유사 + abs(n-array[i]) 갱신논리 (0) | 2022.11.25 |
---|---|
팩토리얼 → ★축약 연산자를 이용한 팩토리얼 (0) | 2022.11.25 |
k의 개수 - IntStream을 Stream<String>으로 변환, 문자열 슬라이싱★★ (0) | 2022.11.25 |
A로 B만들기 → steam을 이용한 문자열정렬★ (0) | 2022.11.25 |
중복된 문자제거 → Set자료형, 메서드 참조, distinct()★★ (0) | 2022.11.25 |