Home
🌿백업
🍏스터디
🥑소통 창구
Subscribe

[JAVA] n 번째 원소부터

작성자
  • 무둥
작성시각
카테고리
  1. JAVA
상태
해결
정수 리스트 num_list와 정수 n이 주어질 때, n 번째 원소부터 마지막 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요.

◇ 내 답변

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;
    }
}

◇ 다른 답변

import java.util.*;
class Solution {
    public int[] solution(int[] num_list, int n) {
        int[] a= Arrays.copyOfRange(num_list, n-1, num_list.length);
        return a;
    }
}
다른 사람 코드.
1.
copyOfRange: 다른 배열의 범위를 복사