문제
정수 리스트 num_list와 정수 n이 주어질 때, num_list를 n 번째 원소 이후의 원소들과 n 번째까지의 원소들로 나눠 n 번째 원소 이후의 원소들을 n 번째까지의 원소들 앞에 붙인 리스트를 return하도록 solution 함수를 완성해주세요.
제한사항
- 2 ≤ num_list 의 길이 ≤ 30
- 1 ≤ num_list 의 원소 ≤ 9
- 1 ≤ n ≤ num_list 의 길이
class Solution {
public int[] solution(int[] num_list, int n) {
int listLen = num_list.length;
int[] answer = new int[listLen];
int idx = 0;
for(int i = n; i < listLen+n; i++) {
answer[idx++] = num_list[i % listLen];
}
return answer;
}
}
n 번째 부터 시작해서 n + numList의 길이만큼 진행하면서 num_list 의 길이를 나눈 나머지값을 구하면 i가 아무리 올라가도 num_list를 계속 순회할 수 있다.
출처
https://school.programmers.co.kr/learn/courses/30/lessons/181891