문제
풀이
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
'Algorithm > Programmers' 카테고리의 다른 글
[Programmers] Lv.0 / n 번째 원소까지 / Java (0) | 2024.01.04 |
---|---|
[Programmers] Lv.0 / 왼쪽 오른쪽 / Java (1) | 2024.01.03 |
[Programmers] Lv.0 / n 번째 원소부터 / Java (0) | 2023.12.11 |
[Programmers] Lv.0 / 배열 조각하기 / Java (0) | 2023.12.07 |
[Programmers] Lv.0 / 2의 영역 / Java (1) | 2023.12.06 |
댓글