[프로그래머스]n의 배수 고르기

박예림·2023년 3월 9일
0

코테

목록 보기
49/132

class Solution {
    public int[] solution(int n, int[] numlist) {
        int j =0;
        int count = 0;
        for (int i =0; i<numlist.length; i++){
            if (numlist[i]%n ==0){
                count++;
            }
        }//배열의 길이를 알 수 없기 때문에 count를 이용해 길이를 먼저 알아낸다.
        int[] answer = new int[count];
        for (int i =0; i<numlist.length; i++){
            if (numlist[i]%n ==0){
                answer[j] = numlist[i];
                j++;
            }
        }
        return answer;
    }
}```
profile
응애 나 아기개발자

0개의 댓글