본문 바로가기
Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 길이에따른연산

by 외계나무 2023. 12. 3.

프로그래머스 - level 0 길이에 따른 연산

 

내 풀이는 그냥 for문 돌린거라 스킵. reduce를 쓰고 싶었는데 어케 쓰는지 아직 감을 못 잡았음.

 

IntStream.of() & stream.sum() / reduce()

import java.util.stream.IntStream;

class Solution {
    public int solution(int[] num_list) {
        IntStream stream = IntStream.of(num_list);
        return num_list.length>10 ? stream.sum() : stream.reduce(1, (a, b) -> a * b);
    }
}

 

Arrays.stream().reduce() / sum()

import java.util.*;

public class Solution {
    public int solution(int[] numList) {
        return numList.length < 11 ? Arrays.stream(numList).reduce(1, (acc, x) -> acc * x) : Arrays.stream(numList).sum();
    }
}