코딩테스트/프로그래머스_코딩 기초 트레이닝

[프로그래머스] n 번째 원소부터

아리빠 2023. 6. 3. 16:05
 정수 리스트 num_list와 정수 n이 주어질 때, n 번째 원소부터 마지막 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요.

 

 

<생각과정> 

 

n번째부터 마지막까지 배열찾기 for반복문, 배열 길이 = 전체 길이에서 -n +1 해주기
class Solution {
    public int[] solution(int[] num_list, int n) {
        
        int answer[] =new int[num_list.length -n+1];
        
        for(int i=n-1; i<num_list.length; i++){
            answer[i-n+1]=num_list[i];
        }
        
        return answer;
    }
}