import java.util.*;
import java.util.stream.*;
class Solution {
public int[] solution(int[] arr, int divisor) {
List<Integer> iList = new ArrayList<>();
for(int i : arr){
if(i % divisor == 0){
iList.add(i);
}
}
int[] a = iList.stream().sorted().mapToInt(Integer::intValue).toArray();
if(a.length == 0){
int[] b = {-1};
return b;
}
return a;
}
}
분석
단순 정렬과 사칙연산 문제
참고할 만한 정답
public int[] divisible(int[] array, int divisor) {
//ret에 array에 포함된 정수중, divisor로 나누어 떨어지는 숫자를 순서대로 넣으세요.
return Arrays.stream(array).filter(factor -> factor % divisor == 0).toArray();
}
비교 분석
filter를 사용하면 앞에서 if문으로 수행한 내용을 동일하게 할 수 있다.
비교 분석 반영
import java.util.*;
import java.util.stream.*;
class Solution {
public int[] solution(int[] arr, int divisor) {
int[] a = Arrays.stream(arr).filter(x -> x % divisor == 0).sorted().toArray();
if(a.length == 0){
int[] b = {-1};
return b;
}
return a;
}
}