본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / n의 배수 고르기 / Java

by unknownomad 2024. 6. 11.

문제

 

풀이

import java.util.Arrays;

class Solution {
    public int[] solution(int n, int[] numlist) {
        int count = 0;
        
        for(int num : numlist) {
            if(num % n == 0) {
                count++;
            }
        }
        
        int[] result = new int[count];
        int index = 0;
        
        for(int num : numlist) {
            if(num % n == 0) {
                result[index++] = num;
            }
        }
        return result;
    }
}
import java.util.Arrays;

class Solution {
    public int[] solution(int n, int[] numlist) {
        return Arrays.stream(numlist)
            .filter(num -> num % n == 0)
            .toArray();
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/120905

댓글