on
[프로그래머스] [Level2] 소수찾기 JAVA
[프로그래머스] [Level2] 소수찾기 JAVA
import java.util. * ;
import java.io. * ;
import java.util.stream.Collectors;
class Solution {
static boolean [] check;
static String [] split ;
static HashSet < Integer > set = new HashSet < > ();
public int solution( String numbers) {
split = numbers. split ( "" );
check = new boolean [ split . length ];
dfs( 0 , "" );
return ( int )set.stream().filter(Solution::isPrime).count();
}
static boolean isPrime( int num){
if (num < 2 ) return false ;
for ( int i = 2 ;i < = ( int )Math.sqrt(num);i + + )
if (num%i = = 0 ) return false ;
return true ;
}
static void dfs( int depth, String cur){
if (depth > split . length ) return ;
for ( int i = 0 ;i < split . length ;i + + ){
if ( ! check[i]){
check[i] = true ;
set. add (Integer. valueOf (cur + split [i]));
dfs(depth + 1 ,cur + split [i]);
check[i] = false ;
}
}
}
}
from http://katastrophe.tistory.com/55 by ccl(A) rewrite - 2021-10-20 15:27:43