inblog logo
|
muaga's Hub
    Programmers-Java

    [프로그래머스] Lv.0 배열의 원소만큼 추가하기 (원소만큼 배열 추가)

    [프로그래머스] Lv.0 배열의 원소만큼 추가하기 (원소만큼 배열 추가)
    muaga's avatar
    muaga
    Jun 05, 2024
    [프로그래머스] Lv.0 배열의 원소만큼 추가하기 (원소만큼 배열 추가)
    Contents
    답풀이
     
    notion image
    notion image
     

    답


    import java.util.Arrays; import java.util.stream.IntStream; class Solution { public int[] solution(int[] arr) { int[] answer = Arrays.stream(arr) .flatMap(n -> IntStream.range(0, n).map(i -> n)) .toArray(); return answer; } }
     

    풀이


    import java.util.Arrays; import java.util.stream.IntStream; class SolutionList { public static void main(String[] args) { int[] arr = { 5, 1, 4 }; int[] answer = Arrays.stream(arr) .flatMap(n -> IntStream.range(0, n) // flatMap : IntStream들을 1개의 Stream으로 결합 // IntStream.range : // 0부터 n-1까지의 숫자를 포함한 IntStream을 생성한다. // [0, 1, 2, 3, 4] + [0] + [0, 1, 2, 3] .map(i -> n)) // [0, 1, 2, 3, 4] -> [5, 5, 5, 5, 5] // [0] -> [1] // [0, 1, 2, 3] -> [4, 4, 4, 4] .toArray(); } }
     
    Share article
    Contents
    답풀이

    muaga's Hub

    RSS·Powered by Inblog