class Solution {
public int[] solution(int[] arr, int k) {
int[] answer = {};
return answer;
}
}
해결법
방법 1
import java.util.*;
class Solution {
public int[] solution(int[] arr, int k) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if(!list.contains(arr[i])) {
list.add(arr[i]);
}
if (list.size() == k) {
break;
}
}
while (list.size() < k) {
list.add(-1);
}
int[] answer = new int[list.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = list.get(i);
}
return answer;
}
}
무작위로 K개의 수 뽑기 Lv. 0