내 답안
import java.util.*;
class Solution {
public List solution(int[] num_list) {
List<Integer> answer = new ArrayList<>();
int last = num_list[num_list.length - 1];
int pre = num_list[num_list.length - 2];
for(int i = 0; i < num_list.length; i++){
answer.add(num_list[i]);
}
if(last > pre){
answer.add(last - pre);
} else {
answer.add(last * 2);
}
return answer;
}
}
Java
복사
다른 사람 풀이법
import java.util.stream.IntStream;
class Solution {
public int[] solution(int[] num_list) {
return IntStream.iterate(0, i -> i + 1)
.limit(num_list.length + 1)
.map(i -> i == num_list.length ? (num_list[i - 1] > num_list[i - 2] ? num_list[i - 1] - num_list[i - 2] : 2 * num_list[i - 1]) : num_list[i])
.toArray();
}
}
Java
복사
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];
answer[answer.length-1] = last > before ? last - before : last*2;
return answer;
}
}
Java
복사