내 답안
import java.util.Arrays;
class Solution {
public int[] solution(int[] arr) {
int min = 100000, max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 2) {
min = Math.min(min, i);
max = Math.max(max, i);
}
}
if (min <= max) {
return Arrays.copyOfRange(arr, min, max + 1);
} else {
return new int[]{-1};
}
}
}
Java
복사
다른사람 풀이
import java.util.*;
class Solution {
public ArrayList<Integer> solution(int[] arr) {
ArrayList<Integer> answer = new ArrayList<Integer>();
int start = -1;
int end = -1;
for(int i = 0; i<arr.length; i++){
if(arr[i]==2){
start = i;
break;
}
}
for(int i = arr.length-1; i>=0; i--){
if(arr[i]==2){
end = i;
break;
}
}
if(start==-1 && end==-1){
answer.add(-1);
} else{
for(int i = start; i<=end; i++){
answer.add(arr[i]);
}
}
return answer;
}
}
Java
복사