반응형
문제설명
정수 n과 정수 배열 numlist가 매개변수로 주어질 때, numlist에서 n의 배수가 아닌 수들을 제거한 배열을 retrun 하도록 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] |
풀이
class Solution {
public int[] solution(int n, int[] numlist) {
int[] array = new int[numlist.length];
int index = 0;
for(int i = 0; i < numlist.length; i++) {
if(numlist[i] % n == 0) {
array[index] = numlist[i];
index++;
}
}
int[] answer = new int[index];
for(int i = 0; i < index; i++) {
answer[i] = array[i];
}
return answer;
}
}
반응형
'알고리즘 > 프로그래머스 LV0' 카테고리의 다른 글
[프로그래머스] Lv0 약수 구하기 (0) | 2022.10.27 |
---|---|
[프로그래머스] Lv0 개미군단 (0) | 2022.10.26 |
[프로그래머스] LV0 최댓값 만들기(1) (0) | 2022.10.25 |
[프로그래머스] LV0 배열 자르기 (0) | 2022.10.25 |