프로그래머스 - 나누어 떨어지는 숫자 배열
Contents
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 |
import java.util.Arrays;
public class Divisible {
public int[] divisible(int[] array, int divisor) {
int[] temp = new int[array.length];
int idx = 0;
for(int i=0;i<array.length;i++) {
if(array[i]%divisor==0) {
temp[idx++] = array[i];
}
}
int[] ret = new int[idx];
for(int i=0;i<idx;i++) {
ret[i] = temp[i];
}
return ret;
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args) {
Divisible div = new Divisible();
int[] array = {5, 9, 7, 10};
System.out.println( Arrays.toString( div.divisible(array, 5) ));
}
} |
아래의 풀이도 있는데 간결해서 깜짝놀라고 속도 떨어지는 것에 또 놀랐다.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays;
class Divisible {
public int[] divisible(int[] array, int divisor) {
return Arrays.stream(array).filter(factor -> factor % divisor == 0).toArray();
}
public static void main(String[] args) {
Divisible div = new Divisible();
int[] array = {5, 9, 7, 10};
System.out.println( Arrays.toString( div.divisible(array, 5) ));
}
} |
Author Jaejin Jang
LastMod 2017-12-23
License Jaejin Jang