나의 풀이
import java.util.*;
class Solution {
public int solution(int n) {
ArrayList<Integer> list = new ArrayList<>();
for(int i =1; i<n+1; i++) {
if(n % i ==1){
list.add(i);
}
}
return list.get(0);
}
}
다른 사람의 풀이
import java.util.stream.IntStream;
class Solution {
public int solution(int n) {
return IntStream.range(2, n).filter(i -> n % i == 1).findFirst().orElse(0);
}
}
'프로그래머스(자바) > LV.1(자바)' 카테고리의 다른 글
콜라츠 추측 (0) | 2022.12.14 |
---|---|
두 정수 사이의 합 → 등차수열의 공식★ (0) | 2022.12.14 |
정수 내림차순으로 배치하기 (0) | 2022.12.12 |
하샤드 수 = 숫자를 문자열로 전환 + 문자열을 숫자로 전환★ (0) | 2022.12.12 |
x만큼 간격이 있는 n개의 숫자 →a[i] =a[i-1] +x (0) | 2022.12.12 |