Skip to content

Commit c3ee713

Browse files
authored
Merge pull request #1688 from AlgorithmWithGod/LiiNi-coder
[20251216] PGM / LV2 / 소수 찾기 / 이인희
2 parents a9fc1e3 + 13ea91f commit c3ee713

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
private static boolean[] visited;
6+
private static HashSet<Integer> set;
7+
private static char[] nums;
8+
9+
public int solution(String numbers) {
10+
nums = numbers.toCharArray();
11+
visited = new boolean[nums.length];
12+
set = new HashSet<>();
13+
14+
15+
dfs("", 0);
16+
int answer = 0;
17+
for(int n : set){
18+
if(isPrime(n)){
19+
answer++;
20+
}
21+
}
22+
return answer;
23+
}
24+
25+
private void dfs(String now, int depth){
26+
if(now.length() > 0){
27+
set.add(Integer.parseInt(now));
28+
}
29+
if(depth == nums.length){
30+
return;
31+
}
32+
33+
for(int i = 0; i < nums.length; i++){
34+
if(visited[i]){
35+
continue;
36+
}
37+
visited[i] = true;
38+
dfs(now + nums[i], depth + 1); // String + char 가능
39+
visited[i] = false;
40+
}
41+
}
42+
43+
private boolean isPrime(int n){
44+
if(n < 2){
45+
return false;
46+
}
47+
for(int i = 2; i <= n / 2; i++){
48+
if(n % i == 0){
49+
return false;
50+
}
51+
}
52+
return true;
53+
}
54+
}
55+
56+
```

0 commit comments

Comments
 (0)