내 답안
class Solution {
public int[] solution(int[] arr, int[][] intervals) {
int length = 0;
for (int i = 0; i < intervals.length; i++) {
int[] group = intervals[i];
length += group[1] - group[0] + 1;
}
int[] answer = new int[length];
int index = 0;
for (int i = 0;i < intervals.length; i++) {
int[] group = intervals[i];
for (int j = group[0]; j <= group[1]; j++) {
answer[index++] = arr[j];
}
}
return answer;
}
}
Java
복사
다른사람 풀이
class Solution {
public int[] solution(int[] arr, int[][] intervals) {
int s1 = intervals[0][0], e1 = intervals[0][1], s2 = intervals[1][0], e2 = intervals[1][1], idx = 0;
int[] answer = new int[e1 - s1 + e2 - s2 + 2];
for (int i = s1; i <= e1; i++) {
answer[idx++] = arr[i];
}
for (int i = s2; i <= e2; i++) {
answer[idx++] = arr[i];
}
return answer;
}
}
Java
복사
import java.util.*;
class Solution {
public int[] solution(int[] arr, int[][] intervals) {
ArrayList<Integer> arrayList = new ArrayList<>();
for (int[] interval : intervals) {
for (int i = interval[0];i <= interval[1];i++)
arrayList.add(arr[i]);
}
int idx = 0;
int[] answer = new int[arrayList.size()];
for (int data : arrayList)
answer[idx++] = data;
return answer;
}
}
Java
복사