Solution 1) 내 풀이
import java.util.*;

class Solution {
    public int[] solution(int[] num_list, int n) {
        List<Integer> list = new ArrayList<>();
        int[] before = Arrays.copyOfRange(num_list, 0, n);
        System.out.println(Arrays.toString(before));
        int[] after = Arrays.copyOfRange(num_list, n, num_list.length);
        System.out.println(Arrays.toString(after));

        for (int i = 0; i < after.length; i++) {
            list.add(after[i]);
        }
        for (int i = 0; i < before.length; i++) {
            list.add(before[i]);
        }
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}
좀 전에 배운 copyOfRange 써봤당

 

Solution 2) 다른 사람의 풀이
class Solution {
    public int[] solution(int[] num_list, int n) {
        int idx = 0;
        int[] answer = new int[num_list.length];
        for (int i = n;i < num_list.length;i++)
            answer[idx++] = num_list[i];
        for (int i = 0;i < n;i++)
            answer[idx++] = num_list[i];
        return answer;
    }
}
걍 반복문이 더 간단했을 문제다. copyOfRange 써봤으니까 정신승리 한다.

+ Recent posts