Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

dearbeany

[프로그래머스] 5명씩 본문

Algorithm

[프로그래머스] 5명씩

dearbeany 2023. 11. 24. 01:54
import java.util.*;

class Solution {
    public String[] solution(String[] names) {
        ArrayList<String> list = new ArrayList<>();
        
        for(int i = 0;  i < names.length; i += 5){
            list.add(names[i]);
        }
        return list.toArray(new String[list.size()]);
    }
}

리스트를 -> 배열로

 

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;
    }
}