class Solution {
public int[][] solution(int[] num_list, int n) {
int[][] answer = new int[num_list.length / n][n];
for(int i = 0; i < num_list.length / n; i++){
for(int j = 0; j < n; j++){
answer[i][j] = num_list[n * i + j];
}
}
return answer;
}
}
- 코드에서의 이중 반복문으로 인해서 불필요한 로직이 생겼고 해당 코드를 아래와 같이 길이의 값을 length를 선언후 저장하여 이중 반복문을 해결 하였다.
class Solution {
public int[][] solution(int[] num_list, int n) {
int length = num_list.length;
int[][] answer = new int[length / n][n];
for(int i = 0; i < length; i++){
answer[i/n][i%n] = num_list[i];
}
return answer;
}
}
https://school.programmers.co.kr/learn/courses/30/lessons/120842
'Algorithm' 카테고리의 다른 글
저주의 숫자 3 (0) | 2023.02.22 |
---|---|
알고리즘 문법 정리 (0) | 2023.02.13 |
가장 큰 수 찾기 (0) | 2023.01.24 |
공간 복잡도 (0) | 2022.08.05 |
알바벳 빈도 수 세기 (0) | 2022.08.05 |