inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 115. 마지막 두 원소

    프로그래머스 (181927)
    Stupefyee's avatar
    Stupefyee
    Feb 28, 2025
    [JAVA 문제 풀이] 115. 마지막 두 원소
    Contents
    내가 작성한 코드
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/181927
    notion image
     

    내가 작성한 코드

    class Solution { public int[] solution(int[] num_list) { // answer 배열을 num_list의 길이보다 1 더 크게 초기화하고 num_list를 복사 int[] answer = Arrays.copyOf(num_list, num_list.length + 1); // num_list의 마지막 원소와 두 번째 마지막 원소를 가져옴 int num_listLast = num_list[num_list.length - 1]; int num_listSecondLast = num_list[num_list.length - 2]; answer[answer.length - 1] = getLastElement(num_listLast, num_listSecondLast); return answer; } // 마지막 원소를 구하는 메서드드 private int getLastElement(int num_listLast, int num_listSecondLast) { return num_listLast > num_listSecondLast ? num_listLast - num_listSecondLast : num_listLast * 2; } }
    Share article
    Contents
    내가 작성한 코드

    stupefyee

    RSS·Powered by Inblog