728x90

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제)정수 리스트 num_list와 정수 n이 주어질 때, num_list를 n 번째 원소 이후의 원소들과 n 번째까지의 원소들로 나눠 n 번째 원소 이후의 원소들을 n 번째까지의 원소들 앞에 붙인 리스트를 return하도록 solution 함수를 완성해주세요.

 

 

 

 

풀이)

class Solution {
    public int[] solution(int[] num_list, int n) {
        int[] answer = new int[num_list.length];
        int idx = 0;
        
        //n부터 num_list까지 일단 우선 배열에 저장
        for(int i=0; i<num_list.length-n; i++){
            answer[i]=num_list[n+i];
        }//0부터 n까지 뒤에다 저장
        for(int i=num_list.length-n; i<num_list.length; i++){
            answer[i]=num_list[idx++];
        }
        return answer;
    }
}
728x90