File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments