Study with me/프로그래머스 L0 마스터하기
프로그래머스 - L0 배열만들기1
외계나무
2023. 12. 18. 11:41
import java.util.*;
class Solution {
public int[] solution(int n, int k) {
List<Integer> answer = new ArrayList<>();
for(int i=1; i<=n; i++) {
if(i%k == 0)
answer.add(i);
}
return answer.stream().mapToInt(Integer::intValue).toArray();
}
}
filter를 이렇게도 쓰는구나...
import java.util.stream.IntStream;
class Solution {
public int[] solution(int n, int k) {
return IntStream.rangeClosed(1, n).filter(i -> i % k == 0).toArray();
}
}