https://school.programmers.co.kr/learn/courses/30/lessons/120905
정수 n
과 정수 배열 numlist
가 매개변수로 주어질 때, numlist
에서 n
의 배수가 아닌 수들을 제거한 배열을 return하도록 solution 함수를 완성해주세요.
1 ≤ n ≤ 10,000
1 ≤ numlist의 크기 ≤ 100
1 ≤ numlist의 원소 ≤ 100,000
n | numlist | result |
---|---|---|
3 | [4, 5, 6, 7, 8, 9, 10, 11, 12] | [6, 9, 12] |
5 | [1, 9, 3, 10, 13, 5] | [10, 5] |
12 | [2, 100, 120, 600, 12, 12] | [120, 600, 12, 12] |
numlist에서 3의 배수만을 남긴 [6, 9, 12]를 return합니다.
numlist에서 5의 배수만을 남긴 [10, 5]를 return합니다.
numlist에서 12의 배수만을 남긴 [120, 600, 12, 12]를 return합니다.
class Solution {
public int[] solution(int n, int[] numlist) {
int[] multiple=new int[numlist.length];
int count=0;
for(int i=0;i<numlist.length;i++){
if(numlist[i]%n==0){
multiple[count]=numlist[i];
count++;
}
}
int[] answer = new int[count];
for(int i=0;i<count;i++){
answer[i]=multiple[i];
}
return answer;
}
}
n
의 배수들만 따로 저장해둘 multiple
배열을 새로 만들고,
만약 numlist
의 각 원소들을 n
으로 나눴을 때 나머지가 0
이면, 저장했다. (배수)