class Solution {
public int[] solution(int n, int[] numlist) {
int[] answer = {};
return answer;
}
}
해결법
방법 1
import java.util.*;
class Solution {
public int[] solution(int n, int[] numlist) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < numlist.length; i++) {
if (numlist[i] % n == 0) {
list.add(numlist[i]);
}
}
int[] answer = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
return answer;
}
}
방법 2
class Solution {
public int[] solution(int n, int[] numlist) {
int count = 0;
for (int i : numlist) {
if (i % n == 0) {
count++;
}
}
int[] answer = new int[count];
int idx = 0;
for (int i : numlist) {
if (i % n == 0) {
answer[idx] = i;
idx++;
}
}
return answer;
}
}
n의 배수 고르기 Lv. 0