Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 조건에맞게수열변환하기3

외계나무 2023. 12. 1. 13:34

프로그래머스 - level 0 조건에 맞게 수열 변환하기 3

▽ Stream을 써보려고 발악한 흔적 ㅋㅋㅋㅋㅋ

import java.util.*;
import java.util.stream.Collectors;

class Solution {
    public int[] solution(int[] arr, int k) {
        int[] answer;
        List<Integer> list = Arrays.stream(arr)
                                    .boxed()
                                    .collect(Collectors.toList());
        if(k%2==0) {
            answer = list.stream()
                        .map(a -> a+k)
                        .mapToInt(Integer::intValue)
                        .toArray();
        } else {
            answer = list.stream()
                        .map(a -> a*k)
                        .mapToInt(Integer::intValue)
                        .toArray();
        }
        return answer;
    }
}

그리고 다시 도움을 받을...
타인의 풀이: Stream ▽

import java.util.*;

class Solution {
    public int[] solution(int[] arr, int k) {
        return Arrays.stream(arr)
                        .map(operand -> k % 2 == 0 ? operand + k : operand * k)
                        .toArray();
    }
}

타인의 풀이: IntStream ▽

import java.util.stream.IntStream;

class Solution {
    public int[] solution(int[] arr, int k) {
        if(k%2==0) {
            return IntStream.of(arr).map(i->i+k).toArray();
        }

        return IntStream.of(arr).map(i->i*k).toArray();
    }
}