보라코딩
프로그래머스 자바 :: 합성수 찾기 본문
합성수 찾기
내 풀이
왜 hashmap이 생각났는지...ㅋㅋ 여튼 풀었다!
import java.util.*;
class Solution {
public int solution(int n) {
int answer = 0;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= n; j++) {
if (i % j == 0) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
}
}
for (Integer a : map.keySet()) {
if (map.get(a) != 1) {
answer++;
}
}
return answer;
}
}
다른사람 풀이
......
class Solution {
public int solution(int n) {
return (int) IntStream.rangeClosed(1, n).filter(i -> (int) IntStream.rangeClosed(1, i).filter(i2 -> i % i2 == 0).count() > 2).count();
}
}
'프로그래머스 (java)' 카테고리의 다른 글
프로그래머스 자바 :: 소인수분해 (0) | 2023.11.17 |
---|---|
프로그래머스 자바 :: 로그인 성공? (0) | 2023.11.17 |
프로그래머스 자바 :: Hash (0) | 2023.11.16 |
프로그래머스 자바 :: 모의고사 (완전탐색) (0) | 2023.10.03 |
프로그래머스 자바 :: 음양 더하기 (0) | 2023.08.29 |