class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer = {};
return answer;
}
}
해결법
방법 1
import java.util.*;
class Solution {
public List solution(int[] num_list, int n) {
List<Integer> answer = new ArrayList<>();
for (int i = 0; i < num_list.length; i+=n) {
answer.add(num_list[i]);
}
return answer;
}
}
방법 2
class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer;
if (num_list.length % n == 0) {
answer = new int[num_list.length / n];
} else {
answer = new int[num_list.length / n + 1];
}
int index = 0;
for (int i = 0; i < num_list.length; i+=n) {
answer[index++] = num_list[i];
}
return answer;
}
}
n개 간격의 원소들 Lv. 0