문제
내가 작성한 정답
import java.util.*;
class Solution {
public int[] solution(int n, int[] numlist) {
return Arrays.stream(numlist).filter(i->i%n==0).toArray();
}
}
import java.util.*;
class Solution {
public int[] solution(int n, int[] numlist) {
ArrayList<Integer> list = new ArrayList<>();
for(int i:numlist){
if(i%n==0) list.add(i);
}
return list.stream().mapToInt(i->i).toArray();
}
}다른 사람들의 정답
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;
}
}Share article