class Solution {
public int[] solution(int[] arr, boolean[] flag) {
int[] answer = {};
return answer;
}
}
import java.util.ArrayList;
class Solution {
public int[] solution(int[] arr, boolean[] flag) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (flag[i]) {
for (int j = 0; j < arr[i] * 2; j++) {
list.add(arr[i]);
}
} else {
for (int j = 0; j < arr[i]; j++) {
list.remove(list.size() - 1);
}
}
}
int[] answer = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
return answer;
}
}
규칙
ArrayList
for문으로 규칙만큼 생성/제거해준다.