inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 111. 등차수열의 특정한 항만 더하기

    프로그래머스 (181931)
    Stupefyee's avatar
    Stupefyee
    Feb 27, 2025
    [JAVA 문제 풀이] 111. 등차수열의 특정한 항만 더하기
    Contents
    내가 작성한 코드다른 사람의 코드
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/181931
    notion image
     

    내가 작성한 코드

    class Solution { public int solution(int a, int d, boolean[] included) { int answer = 0; // included 배열을 순회하면서 true인 경우에만 a + d * i 더하기 for(int i = 0; i < included.length; i++) { if(included[i]) { answer += a + d * i; } } return answer; } }
     

    다른 사람의 코드

    import java.util.stream.IntStream; class Solution { public int solution(int a, int d, boolean[] included) { return IntStream .range(0, included.length) // 0부터 included.length-1까지 반복 .map(idx -> included[idx] ? a + (idx * d) : 0) // included[idx]가 true이면 a + (idx * d), 아니면 0 .sum(); // 모든 값을 더한 합계를 반환 } }
     
    Share article
    Contents
    내가 작성한 코드다른 사람의 코드

    stupefyee

    RSS·Powered by Inblog