본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 순서 바꾸기 / Java

by unknownomad 2023. 12. 11.

문제

 

풀이

import java.util.*;

class Solution {
    public int[] solution(int[] num_list, int n) {
        
        int[] first = Arrays.copyOfRange(num_list, n, num_list.length);
        int[] second = Arrays.copyOfRange(num_list, 0, n);
        
        int[] answer = new int[first.length + second.length];
        System.arraycopy(first, 0, answer, 0, first.length);
        System.arraycopy(second, 0, answer, first.length, second.length);
        
        return answer;
    }
}
import java.util.stream.IntStream;

class Solution {
    public int[] solution(int[] num_list, int n) {
        return IntStream.range(0, num_list.length)
            .map(i -> num_list[(i + n) % num_list.length])
            .toArray();
    }
}
class Solution {
    public int[] solution(int[] num_list, int n) {
        int[] answer = new int[num_list.length];
        
        for (int i = 0; i < num_list.length; i++) {
            if (n == num_list.length) {
                n = 0;
            }
            answer[i] = num_list[n];
            n++;
        }
        return answer;
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/181891

댓글