43. 마지막 두 원소

김해준's avatar
Dec 08, 2025
43. 마지막 두 원소

사용언어 : Java

문제
notion image
풀이
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]; } answer[answer.length - 1] = num_list[num_list.length - 1] > num_list[num_list.length - 2] ? num_list[num_list.length - 1] - num_list[num_list.length - 2] : num_list[num_list.length - 1] * 2; return answer; } }
notion image
문제를 읽었을 때 다소 헷갈릴 순 있으나
매개변수로 주어진 배열의 크기보다 한칸 더 큰 배열인 answer를 준비해 0번 인덱스 부터 복사를 하고 answer의 마지막 인덱스에는 매개변수로 주어진 배열의 마지막 인덱스의 데이터값을 조건에 맞춰 수정한다음 넣어주면 된다
 
Share article