정수 리스트 num_list
가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요.
num_list
의 길이 ≤ 10num_list
의 원소 ≤ 9num_list | result |
---|---|
[2, 1, 6] | [2, 1, 6, 5] |
[5, 2, 1, 7, 5] | [5, 2, 1, 7, 5, 10] |
입출력 예 #1
입출력 예 #2
class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.length + 1];
for(int i = 0 ; i < num_list.length; i++) {
answer[i] = num_list[i];
}
int last = num_list[num_list.length - 1];
int before = num_list[num_list.length - 2];
if (last > before) {
answer[num_list.length] = last - before;
} else {
answer[num_list.length] = last * 2;
}
return answer;
}
}