본문 바로가기

코딩테스트

배열 뒤집기

풀엇음

 

public class ReverseList {
    /**
     * 정수가 들어있는 배열 num_list가 매개변수
     * num_list의 원소의 순서를 거꾸로 뒤집은 배열을 return하도록 solution 함수를 완성하세요
     */
    public int[] solution(int[] num_list) {
        int[] answer = {};
        int length = num_list.length; // 5
        answer = new int[length];

        for(int i = 0; i < num_list.length; i++) { // 0,1,2,3,4
            int num = num_list[i];
            answer[length-i-1] = num;
        }
        return answer;
    }
}

 


 

boxed(), reverse()를 쓴 케이스

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Arrays;

class Solution {
    public int[] solution(int[] numList) {
        List<Integer> list = Arrays.stream(numList).boxed().collect(Collectors.toList());

        Collections.reverse(list);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

'코딩테스트' 카테고리의 다른 글

짝수 홀수 개수  (0) 2026.04.22
문자열 뒤집기  (0) 2026.04.12
나이 출력  (0) 2026.04.10
아이스 아메리카노  (0) 2026.04.07
옷가게 할인 받기  (0) 2026.04.07