공부/2024 항해99코딩클럽
99클럽 3기 코테 스터디 15일차 TIL /[프로그래머스] 소수찾기 자바
푸딩코딩
2024. 8. 5. 23:14
728x90
반응형
1. 오늘의 학습 문제
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42839
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
import java.util.*;
class Solution {
static ArrayList<Integer> arr = new ArrayList<>();
static boolean[] check = new boolean[7];
public int solution(String numbers) {
int answer = 0;
for(int i=0; i<numbers.length(); i++){
dfs(numbers,"",i+1);
}
for(int i=0; i<arr.size(); i++){
if(prime(arr.get(i))) answer++;
}
return answer;
}
//백트래킹
static void dfs(String str, String temp, int m) {
if(temp.length() == m){
int num = Integer.parseInt(temp);
if(!arr.contains(num)){
arr.add(num);
}
}
for(int i=0; i<str.length(); i++){
if(!check[i]){
check[i] = true;
temp += str.charAt(i);
dfs(str, temp, m);
check[i] = false;
temp = temp.substring(0, temp.length()-1);
}
}
}
static boolean prime(int n){
if(n<2) return false;
for(int i=2;i<= Math.sqrt(n) ; i++){
if(n %i ==0)
return false;
}
return true;
}
}
#99클럽 #코딩테스트 준비 #개발자 취업 #항해99 #TIL
728x90
반응형