[JAVA 문제 풀이] 151. 순서 바꾸기

프로그래머스 (181891)
Stupefyee's avatar
Mar 19, 2025
[JAVA 문제 풀이] 151. 순서 바꾸기
notion image
 

내가 작성한 코드

import java.util.stream.IntStream; class Solution { public int[] solution(int[] num_list, int n) { return IntStream.concat( // 두 스트림을 연결 IntStream.of(num_list).skip(n), // n번째 이후 원소 IntStream.of(num_list).limit(n) // n번째까지 원소 ).toArray(); } }
 

다른 사람의 코드

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]) // (i + n) % num_list.length를 사용하여 배열을 벗어나지 않도록 순환 .toArray(); } }
  • 배열을 회전하는 방식
 
Share article

stupefyee