내 답안
import java.util.ArrayList;
import java.util.List;
class Solution {
public String[] solution(String[] names) {
List<String> list = new ArrayList<>();
for (int i = 0; i < names.length; i+=5) {
list.add(names[i]);
}
return list.toArray(new String[0]);
}
}
Java
복사
다른 사람 풀이
class Solution {
public String[] solution(String[] names) {
int idx = 0;
String[] answer = new String[names.length % 5 == 0 ? names.length / 5 : names.length / 5 + 1];
for (int i = 0;i < names.length;i+=5)
answer[idx++] = names[i];
return answer;
}
}
Java
복사
class Solution {
public String[] solution(String[] names) {
String[] answer = new String[(names.length-1)/5+1];
for(int i=0; i<answer.length; i++) {
answer[i] = names[5*i];
}
return answer;
}
}
Java
복사