Coding Test Practice

재귀를 활용하여 배열 순서 뒤집기

마손리 2023. 3. 20. 09:56
class Solution {
    public int[] reverseArr(int[] arr){
        if(arr.length ==0) return new int[0];

        int[] head = Arrays.copyOfRange(arr,arr.length-1,arr.length);
        int[] tail = reverseArr(Arrays.copyOf(arr,arr.length-1));

        int[] result = new int[head.length + tail.length];
        System.arraycopy(head, 0, result, 0, head.length);
        System.arraycopy(tail, 0, result, head.length, tail.length);

        return result;
    }
}