YJ의 새벽

프로그래머스(0) - n의 배수 고르기 본문

코딩테스트연습/프로그래머스

프로그래머스(0) - n의 배수 고르기

YJDawn 2024. 3. 13. 14:20

 

 

 

 

 

class Solution {
    public int[] solution(int n, int[] numlist) {
        int[] answer = {};
        int count = 0;
        int count2 = 0;
        
        for(int i=0; i<numlist.length; i++) {
        	if(numlist[i] % n == 0) {
        		count++;          // 배수의 갯수 추출
        	}
        }
        answer = new int[count];   // 갯수만큼 배열길이 잡아주고
        
        for(int i=0; i<numlist.length; i++) {
        	if(numlist[i] % n == 0) {
        		answer[count2] = numlist[i];  // count2 로 배열길이 ++
        		count2++;
        	}
        }
        
        
        return answer;
    }
}
Comments