dearbeany
[프로그래머스] 5명씩 본문
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;
}
}
'Algorithm' 카테고리의 다른 글
[프로그래머스] 카펫 (0) | 2023.11.24 |
---|---|
[프로그래머스] JadenCase 문자열만들기 (1) | 2023.11.24 |
[프로그래머스] 공백으로 구분하기2 (1) | 2023.11.22 |
[Java] 다익스트라 알고리즘 (프림과의 차이점, 구현) (0) | 2022.10.23 |
[Java] 프림 알고리즘 & 2가지 구현방법 (반복문과 인접행렬, 우선순위큐와 인접리스트) (0) | 2022.10.23 |