프로그래머스 level2 소수 찾기(순열,Hashset)

import java.io.IOException;
import java.util.HashSet;

class Solution {
    static HashSet<Integer> answer = new HashSet<>();
	static int change_number(int[] arr, int r) {
		StringBuilder result = new StringBuilder();
		result.append(0);
		for(int i = 0; i < r; i++)
			result.append(arr[i]);
		return Integer.parseInt(result.toString());

	}
	static void per2(int[] arr, int[] output, boolean[] visited, int depth, int n, int r) {
		prime(change_number(output, depth));
		if(depth == r) {
			return;
		}

		for(int i = 0; i < n; i++) {
			if(visited[i] != true) {
				visited[i] = true;
				output[depth] = arr[i];
				per2(arr, output, visited, depth + 1, n, r);
				visited[i] = false;
			}
		}
	}
	static void prime(int num)
	{
		if(num<2)
			return;
		for (int i = 2; i < num; i++) {
			if (num % i == 0)
				return;
		}
		answer.add(num);
	}
    public int solution(String numbers) {
        int[] arr = new int[numbers.length()]; 
		int n = arr.length; 
		int[] output = new int[n]; 
		boolean[] visited = new boolean[n]; 

		for(int i=0; i<n; i++)
			arr[i] = numbers.charAt(i)- '0';
        
		per2(arr, output, visited, 0, n, n);
        return answer.size();
    }
}

좋은 웹페이지 즐겨찾기