반응형

문제 설명

 

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


제한사항

 

2 ≤ num_list의 길이 ≤ 30

1 ≤ num_list의 원소 ≤ 9

1 ≤ n  num_list의 길이


입출력 예

 

num_list n result
[2, 1, 6] 3 [6]
[5, 2, 1, 7, 5] 2 [2, 1, 7, 5]

입출력 예 설명

 

입출력 예 #1

[2, 1, 6]의 세 번째 원소부터 마지막 원소까지의 모든 원소는 [6]입니다.

 

입출력 예 #2

[5, 2, 1, 7, 5]의 두 번째 원소부터 마지막 원소까지의 모든 원소는 [2, 1, 7, 5]입니다.


문제풀이

 

-> num_list의 length와 n값을 뺀 후 +1을 하면 result의 length가 된다. 이를 통해 answer를 선언하고, for문을 통해서 length만큼 반복하여 index 값을 고려해 num_list의 값들을 담아주면된다.


코드

 

class Solution {
    public int[] solution(int[] num_list, int n) {
        int[] answer = {};
        int length = num_list.length - n + 1;
        answer = new int[length];
        for(int i=0;i<length;i++){
            answer[i]=num_list[n-1+i];
        }
        return answer;
    }
}
반응형

+ Recent posts