#문제
https://programmers.co.kr/learn/courses/30/lessons/42839
#소스코드
import java.util.*;
class Solution {
public ArrayList<Integer> primes = new ArrayList<Integer>();
public int[] numbers;
public boolean[] used;
public int num, count;
public boolean isPrime(int k){
if(k < 2) return false;
for(int i = 2; i <= Math.sqrt(k); i++){
if(k%i == 0){
return false;
}
}
if(!primes.contains(k)){
primes.add(k);
}
return true;
}
public void recurFunction(int num, int[] numbers, boolean[] used){
if(String.valueOf(num).length() == numbers.length){
return;
}
for(int i = 0; i < numbers.length; i++){
if(used[i]) continue;
int newNum = Integer.parseInt(num + "" +numbers[i]);
if(isPrime(newNum)) count++;
used[i] = true;
recurFunction(newNum, numbers, used);
used[i] = false;
}
}
public int solution(String input) {
char[] arr = input.toCharArray();
used = new boolean[arr.length];
numbers = new int[arr.length];
for(int i= 0; i < numbers.length; i++){
numbers[i] = Integer.parseInt(String.valueOf(arr[i]));
}
for(int i = 0; i < numbers.length; i++){
if(numbers[i] == 0) continue;
used[i] = true;
if(isPrime(numbers[i])) count++;
recurFunction(numbers[i], numbers, used);
used[i] = false;
}
return primes.size();
}
}
'💡Problem Solving > Programmers' 카테고리의 다른 글
[프로그래머스] 게임 맵 최단거리 (Java) (0) | 2021.10.23 |
---|---|
[프로그래머스] 뉴스 클러스터링 (Java) (0) | 2021.10.23 |
[프로그래머스] JadenCase 문자열 만들기 (Java) (0) | 2021.10.23 |
[프로그래머스] 오픈채팅방 (Java) (0) | 2021.10.22 |
[프로그래머스] 예상 대진표 (Java) (0) | 2021.10.21 |