프로그래머스 - n개 간격의 원소들

박철현·2023년 5월 19일

프로그래머스

목록 보기
15/80

프로그래머스 - n개 간격의 원소들

import java.util.ArrayList;
import java.util.List;

class Solution {
    public int[] solution(int[] num_list, int n) {
        List<Integer> answer = new ArrayList<Integer>();

        for (int i = 0; i < 20; i++) {
            if (i * n < num_list.length)
                answer.add(num_list[i * n]);
            else
                break;
        }
        
        return answer.stream()
                .mapToInt(Integer::intValue)
                .toArray();
    }
}
  • n개 간격 -> i*n
  • Stream.mapToInt(Integer::intValue) => Int Stream으로 변환시키는 메소드+ Integer클래스의 intValue 메서드(Integer 객체에서 int값 추출)를 각각 적용시킴
profile
비슷한 어려움을 겪는 누군가에게 도움이 되길

0개의 댓글