내 답안
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;
}
}
Java
복사
다른사람 풀이
import java.util.stream.Stream;
class Solution {
public int[] solution(int[] arr, boolean[] flag) {
String answer = "";
for(int idx=0; idx<arr.length; idx++) {
if(flag[idx]) {
answer += String.valueOf(arr[idx]).repeat(arr[idx]*2);
}
else {
answer = answer.substring(0, answer.length() - arr[idx]);
}
}
return Stream.of(answer.split("")).mapToInt(Integer::parseInt).toArray();
}
}
Java
복사