프로그래머스 문제풀이/Level 0

n의 배수 고르기 (자바, Java)

뮤츠 2022. 11. 26. 23:43

ArrayList로 풀어도 되지만,

그냥 Queue를 써서 풀었다. 크기가 가변적인 경우에는 ArrayList가 그렇게까지 효율적이지는 않다고 알고 있다.

 

import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int[] solution(int n, int[] numlist) {
        int[] answer = {};
        Queue<Integer> queue = new LinkedList<>();
        for (int i : numlist) {
            if (i%n==0) {
                queue.add(i);
            }
        }
        
        answer = new int[queue.size()];
        for (int i=0; i<answer.length; i++) {
            answer[i] = queue.poll();
        }
        
        return answer;
    }
}