[JAVA 문제 풀이] 163. 길이에 따른 연산

프로그래머스 (181879)
Stupefyee's avatar
Mar 27, 2025
[JAVA 문제 풀이] 163. 길이에 따른 연산
notion image
 

내가 작성한 코드

class Solution { public int solution(int[] num_list) { int answer = 1; if(num_list.length > 10) { answer -= 1; // 총합은 기본 값 빼주기 for(int i : num_list) { answer += i; } } else { for(int i : num_list) { answer *= i; } } return answer; } }
 

다른 사람의 코드

import java.util.*; class Solution { public int solution(int[] num_list) { if(num_list.length > 10) { return Arrays.stream(num_list).sum(); } else { return Arrays.stream(num_list).reduce(1, (a, b) -> a * b); } } }
 
Share article

stupefyee