내 답안
import java.util.*;
class Solution {
public List solution(int[] arr) {
List<Integer> answer = new ArrayList<>();
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i]; j++){
answer.add(arr[i]);
}
}
return answer;
}
}
Java
복사
다른사람 풀이
class Solution {
public int[] solution(int[] arr) {
int[] answer = {};
int sum = 0;
for(int i=0; i<arr.length; i++) {
sum += arr[i];
}
answer = new int[sum];
int idx = 0;
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i]; j++) {
answer[idx++] = arr[i];
}
}
return answer;
}
}
Java
복사