Make Be BackEnd
[JAVA] n보다 커질 때까지 더하기 본문
class Solution {
public int solution(int[] numbers, int n) {
int answer = 0;
for(int i : numbers) { answer+= i; if(answer>n){ break; } } return answer;
}
}
import java.util.Arrays;
class Solution {
public int solution(int[] numbers, int n) {
return Arrays.stream(numbers).reduce(0, (acc, i) -> n >= acc ? acc + i : acc);
}
}
reduce !!
Stream (Java SE 14 & JDK 14)
Type Parameters: T - the type of the stream elements All Superinterfaces: AutoCloseable, BaseStream > public interface Stream extends BaseStream > A sequence of elements supporting sequential and parallel aggregate operations. The following example illustr
download.java.net
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[JAVA] 부분 문자열인지 확인하기 (0) | 2024.03.11 |
---|---|
[JAVA] 문자열의 앞의 n글자 (0) | 2024.03.10 |
[JAVA] n보다 커질 때까지 더하기 (0) | 2024.03.10 |
[JAVA] 정수 찾기 (0) | 2024.03.09 |
[JAVA] n의 배수 고르기 (0) | 2024.03.09 |