문제
내가 작성한 정답
class Solution {
public int[][] solution(int[] num_list, int n) {
int len = num_list.length;
int[][] answer = new int[len/n][n];
for(int i=0; i<len; i++){
answer[i/n][i%n] = num_list[i];
}
return answer;
}
}
class Solution {
public int[][] solution(int[] num_list, int n) {
int len = num_list.length, idx = 0;
int[][] answer = new int[len/n][n];
for(int i=0; i<len/n; i++){
for(int j=0; j<n; j++){
answer[i][j] = num_list[idx++];
}
}
return answer;
}
}다른 사람들의 정답
import java.util.stream.IntStream;
class Solution {
public int[][] solution(int[] num_list, int n) {
return IntStream.range(0, num_list.length / n)
.mapToObj(i -> IntStream.range(0, n)
.map(j -> num_list[i * n + j])
.toArray())
.toArray(int[][]::new);
}
}
Share article