Develop

[프로그래머스] 등차수열의 특정한 항만 더하기 본문

개인 공부/프로그래머스

[프로그래머스] 등차수열의 특정한 항만 더하기

개발 기록 2024. 1. 18. 18:02

문제

 

내풀이

class Solution {
    public int solution(int a, int d, boolean[] included) {
        int answer = 0;
        for(int i=0; i < included.length;i++){
            answer += (included[i]? a+(d*i) : 0);
        }
        return answer;
    }
}

 

 

다른 풀이

import java.util.stream.IntStream;

class Solution {
    public int solution(int a, int d, boolean[] included) {
        return IntStream.range(0, included.length).map(idx -> included[idx]?a+(idx*d):0).sum();
    }
}

 

느낀점

 

stream을 쓰니까 한줄로 되네..

코드를 보면 이해는 되지만 직접 쓰기엔 아직 부족하다

람다식도 공부해야겠다