[알고리즘문제풀기] 순서 바꾸기

silver's avatar
Aug 23, 2025
[알고리즘문제풀기] 순서 바꾸기

문제

내가 작성한 정답

import java.util.Arrays; import java.util.stream.IntStream; class Solution { public int[] solution(int[] num_list, int n) { int[] a = Arrays.copyOfRange(num_list,0,n); int[] b = Arrays.copyOfRange(num_list,n,num_list.length); return IntStream.concat(Arrays.stream(b),Arrays.stream(a)).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(i>=n) answer[i-n] = num_list[i]; else answer[num_list.length-n+i] = num_list[i]; } return answer; } }

다른 사람들의 정답

import java.util.stream.IntStream; class Solution { public int[] solution(int[] num_list, int n) { return IntStream.range(0, num_list.length) // 인덱스 i에 n을 더한 후 배열 길이로 나눈 나머지 인덱스를 통해 원본 배열 순환 접근 // 즉, n번째 원소 이후의 부분부터 시작해서 배열 끝까지, 그리고 배열 처음부터 n-1까지 이어붙인 효과를 냄 .map(i -> num_list[(i + n) % num_list.length]) // 결과 스트림을 배열로 변환하여 반환 .toArray(); } }
Share article

silver