내 답안
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;
}
}
Java
복사
다른사람 풀이
class Solution {
public int[] solution(int[] num_list, int n) {
int N = num_list.length % n == 0 ? num_list.length / n : num_list.length / n + 1;
int idx = 0;
int[] answer = new int[N];
for (int i = 0;i < num_list.length;i+=n)
answer[idx++] = num_list[i];
return answer;
}
}
Java
복사
class Solution {
public int[] solution(int[] num_list, int n) {
Double length = Math.ceil(num_list.length/(n*1.0));
int[] answer = new int[length.intValue()];
for(int idx=0; idx<length; idx++) {
answer[idx] = num_list[idx*n];
}
return answer;
}
}
Java
복사